Trending topics
#
Bonk Eco continues to show strength amid $USELESS rally
#
Pump.fun to raise $1B token sale, traders speculating on airdrop
#
Boop.Fun leading the way with a new launchpad on Solana.
Why should you learn recursion even if you will never use it?
Recursive solutions train you on a few mindsets:
1. Rather than trying to generate the solution, you often start with “what is the structure of a valid solution” work backwards. For some problems, working backwards is much easier.
2. When you are solving the problem, it’s easy to get distracted by all the “what ifs.” When solving a problem recursively, you often forced to “ignore” 90% of the issues and focus on getting just one part right.
3. What would often be a “corner” case in an imperative solution is a “base case” in a recursive on. Thinking recursively sometimes forces you to not ignore the corner cases. Furthermore, recursive solutions make heavy use of pattern matching so you are forced to think of all the situations you could encounter.
Here’s a really good example: Leetcode 335 Self Crossing (Hard problem).
You travel on a spiral trajectory on a grid (i.e. always turn left after traveling some distance north, south, east, or west). The question is, “given the distance of each ‘segment’ of the spiral in order, did the spiral cross itself or not?”
Although the solution to this doesn’t need to be a function calling itself, the “nice” solution uses recursive properties:
1. if we haven’t found a crossing yet, then we can assume there are no crossings or invalid spirals in the past. We furthermore notice that it doesn’t matter if we are traveling left, right, up, or down because we can only turn left. All we care about is if the previous segments are parallel to our previous turn and how far away they are.
2. when we turn left, there is an extremely limited number of “segments” in the spiral we can crash into, which is “recursively” true no matter how big the spiral gets. There are a lot of past data about the spiral we can ignore.
3. There’s a limited number of scenarios in your previous turn that affect your logic: a) did you travel far enough to not crash into anything, b) if not, what could you potentially crash into? (also limited).
The annoying thing about Leetcode hards is they suddenly become easy if you find the key insight. But those key insights will come to you more naturally if you’ve trained yourself in recursive programming.
It’s not just about designing functions that call themselves — it’s about forcing yourself to break the problem down in such a way that it can be solved with a function calling itself. The more ways you can break down a problem, the more likely you are to find an “aha” solution.
Obviously, I don’t need to leetcode in my profession, but I do need to find creative ways to break down problems so they become understandable — and training in recursion has definitely helped with that.

1.66K
Top
Ranking
Favorites