My Guide to mastering Data Structures and Algorithm
I have been programming for more than a decade now. Helped small Start-ups to huge enterprise teams. When it comes to DS & Algo, there is abundance of online resources out there and you don’t need to read yet another “How to guide” for this topic.
However, I am writing this guide for “My” students and devs joining “My” network. I take accountability for my recommendations here. Just like how my mentors guided me in my programming journey and set me up for success, I am writing this to help you with your journey.
This article is a “Mental Model Guide”. So,
- I will not be sharing the “What’s” but just the “How’s”,
- Most importantly, The 3 Step process to solve any DSA problem.
Few notes about DS & Algos, before we start
- These highly optimised DS & Algos you see everyday, took years to develop by some of the best minds working on that problem. So, don’t feel depressed if you can’t get it the first time you read it, cause the person who wrote it, spent years refining it.
- DS & Algos comes easily to those inquisitive minds who enjoy puzzles and problem-solving. In my case, I loved problem solving, still these problems would scare the life out of me. I couldn’t even visualise the algorithm iterations and it was giving me brain freeze. To fix it, I started with a lot of super simple problems for many weeks and worked my way up. This fixed my initial brain freeze issues.
- No matter how difficult the problem, we don’t cheat. We start with the simplest dumbest code to solve that problem. We only take reference once we have taken a stab at the problem from different angles.
My philosophy on DSA is that — “You shouldn’t be focused on DSA too much. Fall in love with programming first and eventually there will be this hunger to make your code faster & memory-optimised. That’s the stage where you organically absorb these DSA concepts. So, instead of jumping into competitive coding, try to build something first. Find your reason to code everyday. “
Now, Let’s get to business
Every Data Structure and Algorithm problem can be solved if we follow these 3 steps correctly. Take one step lightly and you’ll suffer!
Let’s unpack these 3 steps.
Step 1: Understand the Problem
When we come across a DSA problem, we read through it, understand the given input and expected output. We try it out in pen and paper.
We do not move to the next step unless given some random input we can find the expected output in pen & paper ourselves. This shows that we have a clear understanding of the problem statement.
Therefore, to validate if Step one is complete, we take random inputs and find out the expected outputs. If it is correct every time we can move to step two.
Step 2: Break it into stages
If we solve the problem in pen & paper multiple times during step one, we start seeing the stages appear. Note down these stages and they will become your building blocks for the final solution.
The trick here is to keep breaking the stages unless you feel, you can write a function to replace that stage in Step three. Also don’t worry about the loops-recursions here, just write the bare minimum stupid stages to break the problem. It doesn’t matter at all if our stages are stupid and slow, we’ll keep on refining it in the future.
Always remember that there are dozens of ways to solve that problem. If we do not try to solve it “our way”, then Google will show us the most optimised but difficult way of doing it. We’ll never be able to solve the same problem ourselves and these problems will keep coming back in different flavours to bully us. If we don’t try the stupid ways first then there will never be any respect for the smart ways.
Not able to identify all the stages? Go back to step one and solve it again & again with different inputs. Move to step three only if you feel confident about writing code for all the stages you have just discovered.
Step 3: Write Code for those stages
This is the easiest and most rewarding stage of all three!
If you have been coding daily for past few weeks then this stage will tickle your brain and your solution will start emerging as code smoothly. This is so satisfying that sometimes we stay lost in this bliss for days.
If you are constantly struggling to convert those stages to code then QUIT that problem right away or you will hurt yourself or even worse you’ll cheat or stay depressed for days. It’s not worth it to break our daily coding rhythm for some problem. After quitting that problem, come back to basics and work on your personal projects to stay motivated. Meanwhile, find online resources and stories related to that problem you quit. Join some online programming community chat rooms or talk to your mentors about that problem. Basically, you’re trying to understand your enemy here and slowly but surely weaponizing yourself with missing concepts to beat that problem someday. No need to rush ourselves here, if you are coding daily on something, I personally guarantee that someday in the next few weeks you WILL feel a strong urge to come back to that problem and solve it.
Conclusion
These three steps are simple but has the power to make or break you. I followed it religiously and you know the trick lies in Step 3, to ignore the problem and keep coding regardless. That’s how I was able to develop this strong and confident mental model, where I am never scared of jumping into a problem solving session. I hope this guide helps you get out of your DSA fear and reverse bully the problems.
Always remember, you’re struggling because either one of three is happening.
- You haven’t been coding everyday.
- You didn’t start with super simple problems and skipped to tough ones.
- You didn’t do your homework on your enemy and might be fighting it alone.
Ending notes
You should be knowing this already by now that to get ahead in life we have to face our fears HEAD-ON. It’s like —
- Someday, I will publish my website and create my app. No! I ll start today.
- I am scared of the terminal and don’t know the commands. No! It’s now or never, I’ll learn the basics of a CLI and make the terminal my best friend.
- These Greedy algorithm problems are still giving me a hard time. No! I’m gonna pick a fight with a greedy problem everyday and beat it unless it gives up.
- I am not ready for this job or promotion yet. No! I ll get into that interview somehow and what’s the worse that can happen.
When you have this continuous confident attitude for months, you leave your competition far behind without ever realising it.