Problem solving is the essential skill. So what is the good way to improve this skill? So many developers with good problem solving skill tell us that we need to train this one on daily basic. However, we can't actually do that every day. If we practise on http://hackerrank.com we see that there are a bunch of problems. We keep solving problems every day though. We also have no idea about what problem the interviewers will challenge us. So what is the right and intelligent way to train this skill? Remember that we solve problems not for keeping them in mind, adding them in the library in our brain. We keep solving problems every day just because we want to train our brain, remembering the solution is just the step after that, in order to improve the connections in the brain.
- Training brain.
- Gaining the patterns.
That is we need to collect the common rules that we can use to solve any problems. Speaking of the solutions, if we break the solution down we can see that solution are just the collection of simple steps, a set of steps to accomplish a certain task. We can't just keep solving problems every day. We should master some common problem solving patterns and practise with the approaches and patterns on daily basic, not focusing mainly on problems.
According to what I learned from problem solving and my experience, I summarize the common approach like this.
- Understand the problem.
- Explore concrete examples.
- Break it down.
- Look back and refactor.
Understand the problem
- Restate the problem in our own words.
- Identify the inputs.
- Identify the outputs.
- The relations between inputs and outputs => enough info to solve the problem?
- Important pieces of data?
Explore the concrete examples
Furthermore, we understand the problem better through the examples. Those are the user stories, the test cases. Hence, we should follow these steps.
- Simple examples.
- Complex examples.
- Examples with empty inputs.
- Examples with invalid inputs.
This approach is very important. I remember the time when I studied Dijkstra algorithm I watched many videos explaining the algorithm but I couldn't understand clearly until I decided to take examples and solve that problem in paper - not exact paper (lol).
Break it down
Writing out the steps explicitly that we need to take. Thinking about our code before writing.
If we can't solve the problem instantly. we simplify the problem. Like I said that the solution is just a set of steps. We can't think of the whole problem. Our brain will get cloudy. Let's break it down like above step. We can't solve it because we couldn't break it smaller enough. The hard difficulty alway solved by a set of simplified steps. We have to complete these simplified steps first. It's like we make interfaces. Then we delegate them to solve without thinking of what they will do in details. Thus, the core difficulty will become simple.
Even we solved it, it doesn't mean it was done. This step is crucial step which help us to perfect our approach and the rules of problem solving. We can't just solve it and forget it right away, can we?
Some questions we can learn from our task:
- Can we understand it at first glance?
- Can we re-use the result or methods for some other problems?
- Can we improve the performance more better?
- Can we solve it in other ways?
- How have other people solved this problem?
Next article I will show you how to apply this approach to a concrete problem.