-
Notifications
You must be signed in to change notification settings - Fork 238
Solving coding challenges
In 1945 Hungarian mathematician George Pólya, described famous method of problem-solving:
- First, we have to understand the problem
- After understanding make a plan
- Carry out the plan.
- Look back on your work. How could it be done better?
This may sound a bit abstract, so let's translate this method into more concrete steps.
- Can I rephrase problem using your my own words?
- What are the inputs?
- What are the outputs?
- How should I name the important pieces of data that are a part of the problem?
- Explore concrete examples
- Start with simple examples and progress to more complex ones
- Explore empty inputs
- Explore invalid inputs
Write exact, atomic steps we need to take to solve the problem. They can be written on a piece of paper or as comments in the file containing code.
This forces you to think about the code you'll write before you write it and helps you catch any issues or misunderstandings before you dive into details (e.g. language syntax).
Now when you have to concede defined steps solving the problem should be much easier. However, if you struggle to get back to previous steps to make sure that you have a good understanding of the problem and you know exactly what to do. You can also try a few alternative approaches such as solving a similar problem or solve a simplified version of the current problem by temporally excluding the most difficult part.
- Try to refactor the code
- Improve performance (Big-O notation)
- Check how other people solved this problem
This strategy is inspired by Colt Steele.
Deliberate (Kata) practice is a great supplement of the above strategy. It involves attention, reworks and leads to new knowledge and skills that can later be developed into more complex knowledge and stronger skills.
Let's take a look at the concrete application of this:
- Solve a coding puzzle
- Compare your solution with the solution with this repository (or other solution on the internet)
- Think about how both solutions differ and what can be improved.
- Solve coding puzzle again (without looking at the ordinal solution) and compare...
Perform above steps multiple times over a period of few days/weeks/months. Bit by bit your mind will rewire itself, you will have a better understanding of the problem/context and code challenge will simply feel familiar to you over time. Do it until solving the problem will be easy like adding 97 and 3.