Here are the steps I have been using to learn new algorithms, which is basically the same strategy as this.
- Pick a problem that shows up frequently in interviews from LeetCode
- Attempt to solve it
- After being stuck for too long, learn the ideal solution by doing the following steps
- Watch videos, i.e. Tushar Roy, Geeks for Geeks
- Read solutions from LeetCode especially the highest voted solutions
- Pick a solution and type it out. Add comments, change the variable names to names that are meaningful to me
- Attempt to solve this problem every day. What this really means is type out the solution every day until I either have it memorized and/or understand it so well that I don’t need to have it memorized, or some combination. Typing the code out every day is great because I am gaining exposure to the code feeling (typing), seeing, and hearing. I think this multi-sensory exposure also gets the information to my subconscious mind so it can spot patterns.
Doing this is key for certain common algorithms such as level-order traversal, iterative in pre and post order binary tree traversals, and also for understanding how to use recursion with base cases and recursive cases.