When I started, I would spend 2 hours solving a single problem.
Spend 1-2 weeks on a topic, learn the data structure and basic algos behind it, then switch to another, and return to the previous topic again but later. Start with strings/arrays, move to linked lists, trees, and graphs, revisit topics over and over, learn dynamic programming concepts solve 1-2 weeks again.
General Problem-Solving Approach
- Read the problem, repeat it, understand it. Don’t immediately jump into coding!
- Understand inputs and outputs. Draw some examples on paper.
- Clarify requirements, ask clarifying questions, and understand constraints (edge cases). Example questions: Is it ASCII or Unicode? What is the max value? Is there a difference between capital letters and small letters?
- Think about the solution in your mind. Divide problems into sub-problems. Come up with different ideas (ask whys, think about trade-offs, solve simpler versions, imagine helper functions - go from high level to low level).
- Evaluate the complexity and trade-offs.
- Think of a better alternative solution.
- Write code on paper.
- Debug your code on paper and test with new corner case inputs.
- Write code. Write clean code.
- Write tests. Positive, negative, with edge-cases.
Check my questions lists here.
More to read:
My Approach to Solve Difficult Problems
- Draw examples on paper, window, whiteboard. Understand the input and output. Understand the logic of how the data changes over iterations, don't write code; just focus on understanding the problem.
- Think about how you would solve this, and consider writing pseudocode. Use imaginary functions or helper functions to make the problem easier. Solve problem high level, to low level.
- Solve simpler problem, remove some constraints of the same problem.
- Still stuck? Watch the solution, and try to understand every every every single step. Understand the implementation/idea, but not the code. Don't try to memorize code.
- Solve similar problems.