sushil@home:~$

Analogy of Problem solving in DS/Algo

If you are struggling in solving algorithm problems, then the following analogy might help you !!

Do you know why restaurants are able to cook food quicker than us making at home? It’s because they have most of the ingradients ready and are just putting them together to give you a warm/freshly cooked dish. Sometimes the whole thing is cooked already and they are just microwaving it for you.

We cant memorize 1000’s of leetcode questions. However we can memorize the patterns that are common in those problems. When I’m able to solve a problem quickly, its because I can recognize what kind of problems it is e.g., two pointer approach, BFS/DFS, sliding window, etc. Also for each pattern, there are some common parts to the solution that we can memorize as well so we can quickly code it up. 45 mins are not enough to invent something new. We are just putting things like just cooks in a restaurant. Most of the time what is changing is just conditions and special cases. Our pattern recognition improves when we harvest the thought process of others. That can be done by mock interviews. Do lots of them and look for patterns.

At times you may struggle in finding solution to a problem because you can’t figure out what algorithms you can apply or what common pattern this problem maps to. However God has given our brain the capability to naturally solve much more complex problems than what we see in an interview. When I can’t figure out the way to solve the problem, I stop thinking of algorithms and data structures and simply visualize the problems as if it is a real life situation and figure out what I would have naturally done to find the solution. Once I’ve figured out the way to solve the problem, I look for appropriate data structures and algorithms I can use to translate it into code. A good example of such problem is “Trapping Rain Water” problem on leetcode. It is a “hard” problem even though the solution doesn’t require any advance knowledge of data structures or algorithms. If you stop tryng to create an algorithm, your brain can intuitively figure out any point the level of water is min of the two max heights on left and right. I call this technique, visually solving the problem. I hope this analogy may have help you in problem solving in DS/Algo.