Sitemap
Geek Culture

A new tech publication by Start it up (https://medium.com/swlh).

How to get good at Recursion

For your FAANG Interviews, Work, or Competitive Coding

6 min readMar 19, 2022

--

Recursion is one of those topics tons of people struggle with. Among the many people I’ve interacted with- through my newsletter, tutoring, or in person- it is consistently amongst the topics that people struggle with the most. Recursion somehow seems both extremely trivial and very unnatural. It is a crucial topic, however. You need it for both your coding interviews and in your tech journeys. Not only is recursion itself important, but many Data Structures, Algorithms, and paradigms (backtracking, dynamic programming) all rely on recursion. Thus, you must get good at it.

This is a relatively common problem for many people. If you struggle with these topics, check out my newsletter https://codinginterviewsmadesimple.substack.com/, proven to get results.

Fortunately, there is a guaranteed way to get better at Recursion. This will work for you, no matter how much you’ve struggled with it so far. This technique goes back to the roots of recursion and will prime your mind to think in a recursive manner. It will save you a lot of time when compared to the standard “Just do lots of recursion questions, bro” you will hear. Let’s get into it.

Why people Struggle with Recursion.

Our minds are not inherently geared to think recursively. Let me give you a simple example. Imagine I asked you to sum the elements of a list. Most people will write something like:

def sum(lst):
sum=0
for i in lst:
sum+=i
return sum

Which is a perfectly fine way of doing it. I’d be willing to bet none of you thought of this:

def sum(lst, sum=0):
if lst==null:
return sum
else:
return (lst[1:], sum+lst[0])

Why not? Simply put, it’s not natural to our way of thinking. Iteration and looping align with our way of processing the world much closer. However, this can come up to bite us when we don’t prepare. Recursive solutions can be very elegant, especially when comes to certain DSA related to trees, graphs, and backtracking. Not to mention, there are certain things that can’t be done with simple loops (there’s actually a really interesting story about this that I will be covering, so sign up to the mailing list to stay updated). So what is the silver bullet that will make you a recursion badass?

Enter Functional Programming

Get ready for a heck of a trip. Because things are about to get very very weird. I’m about to introduce you to a way of programming that completely broke me. It is called Functional Programming. You can watch this excellent video here to get into the details.

Press enter or click to view image in full size
Look at all the jargon. There is a lot of theory behind functional programming, which can make it overwhelming without somebody experienced to guide you.

I won’t get too much into the technical details about why functional programming is different. I will tell you about some important ways, however:

  1. Functional Programming has no variables. Absolutely none. We declare the values, and then call some functions on them. This is done so that we don’t change the ‘state’ of the machine.
  2. We can pass functions as parameters to other functions. This can be very powerful and allows for very small compact codes.
  3. There is no looping. Functional Programming solves everything with recursion.
  4. Functions in functional programming are treated like functions in Math. They take an input and return some output. That’s it.

If I just got into the technical details of functional programming, I would need hours explaining the nuances. Instead, let’s keep things simple, and instead focus on how you can use it to improve your recursion skills.

Functional Programming for better recursion

Time to get to the climax of this article. How you can get good at recursion. The first step would be to pick a functional language. I would personally recommend either Lisp (it will help you if you want to get into AI) or Haskell (it’s used by a couple of big firms including Facebook). It’s fine if you find another functional language better to use. Any of them will work for our purposes.

Next, try writing out some interesting functions in the functional style. I would suggest writing a few codes in the functional style. This will have you thinking in the functional way. I would recommend looking into the following:

  1. Sorting, Searching, Reversing, and Length (of) a List. Merging lists is another good one.
  2. List Math. Includes adding values to all values in a list, summing the elements of a list, etc etc.
  3. Factorial, Ackermann, and other classically recursive functions.

These will be a good foundation to improve your base. They might seem trivial, but trust me, doing them in a functional way will help you a ton. It will create a solid foundation of recursive logic that will let you learn a lot from your Leetcode questions.

Press enter or click to view image in full size
This might seem very strange at first, but functional programming is great for building recursive logic.

This is something that I’ve told all my students to implement in their coding interview preparation. The results speak for themselves:

One of many success stories. Check out my other articles if you are preparing for your coding interviews.

To get the best results, check out my daily newsletter, Coding Interviews Made Simple. It covers topics in Algorithm Design, Math, Recent Events in Tech, Software Engineering, and much more. You can check out the schedule here. The premium subscription costs only 33 cents USD(INR 26) per day and has achieved great results.

This had amazing ROI. Subscribe now for a 20% discount

I created Coding Interviews Made Simple using new techniques discovered through tutoring multiple people into top tech firms. The newsletter is designed to help you succeed, saving you from hours wasted on the Leetcode grind. Join a community of coders improving their skills and acing their interviews by subscribing.

The newsletter has helped a ton of people succeed in tech

To help me write better articles and understand you fill out this survey (anonymous). It will take 3 minutes at most and allow me to improve the quality of my work.

Feel free to reach out if you have any interesting jobs/projects/ideas for me as well. Always happy to hear you out.

For monetary support of my work following are my Venmo and Paypal. Any amount is appreciated and helps a lot. Donations unlock exclusive content such as paper analysis, special code, consultations, and specific coaching:

Venmo: https://account.venmo.com/u/FNU-Devansh

Paypal: paypal.me/ISeeThings

Reach out to me

Use the links below to check out my other content, learn more about tutoring, or just to say hi. Also, check out the free Robinhood referral link. We both get a free stock (you don’t have to put any money), and there is no risk to you. So not using it is just losing free money.

Check out my other articles on Medium. : https://rb.gy/zn1aiu

My YouTube: https://rb.gy/88iwdd

Reach out to me on LinkedIn. Let’s connect: https://rb.gy/m5ok2y

My Instagram: https://rb.gy/gmvuy9

My Twitter: https://twitter.com/Machine01776819

If you’re preparing for coding/technical interviews: https://codinginterviewsmadesimple.substack.com/

Get a free stock on Robinhood: https://join.robinhood.com/fnud75

--

--

Devansh
Devansh

Written by Devansh

Writing about AI, Math, the Tech Industry and whatever else interests me. Join my cult to gain inner peace and to support my crippling chocolate milk addiction

Responses (1)