Think like a programmer. Problem solving lesson
If you are interested in programming, you may have heard the phrase:
“Everyone has to learn programming, because it teaches thinking.”
- Steve Jobs
Perhaps you also wondered what it means to think as a programmer?
In fact, we are talking about a more efficient way to solve problems.
This post aims to teach you this.
After reading it, you will more accurately understand what needs to be done to find the best solutions.
Why is it important?
Problem solving is a basic skill.
We are constantly solving problems. Big and small. How do we do it? Sometimes it's good ... if you're lucky.
If you do not have a systematic approach, then you probably solve problems as follows (I did this when I first started coding):
- try the solution
- if it doesn't fit, then try another
- if it didn’t work, repeat point 2 until the victory.
You may be lucky, but this is the worst way! And it can take a lot of time.
The best way:
- have a systematic approach
- put it into practice.
“Most employers consider problem solving skills to be more important.
The ability to solve problems almost unanimously is the most important ability that employers are looking for ... More important than knowledge of programming languages, debugging and system design.
Demonstrating integrated thinking and the ability to solve big challenges are just as valuable (if not more) than the basic technical skills needed to work. ”
- Hacker Rank ( 2018 Developer Skills Report )
To find the right approach, I followed the advice from the book by Tim Ferriss about learning " The 4-Hour Chef ".
This led me to an interview with two really impressive people: C. Jordan Ball (ranked 1st or 2nd place out of 65,000 Coderbyte users ) and V. Anton Spraul (author of Think Like a Programmer. A creative approach to creating code. ").
I asked them the same questions, and guess what? Their answers were very similar!
Soon you will recognize them too.
Note: this does not mean that they all do the same. They are different people, and you are different from them. But if you start with the correct basic principles, the result will be much better and much faster.
“The most serious mistake I see among young programmers is a focus on learning syntax, instead of learning how to solve problems.”
- V. Anton Spraul
So what to do when you face a new challenge?
We will analyze in steps:
Understand what specifically needs to be done. Most tasks are difficult because you don’t understand them (which is why this is the first step).
How do I know that you understand the essence of the problem? Try to explain it in simple language.
Remember the case when you stuck on a task and started explaining it to yourself. At this moment logical errors appear that you have not seen before.
Most programmers are familiar with this feeling.
That is why you should describe your task, draw a diagram or tell someone about it (some use a rubber duck ).
“If you cannot explain something in simple terms, then you do not understand it.”
- Richard Feynman
Do not start solving a problem without a plan, hoping not to get lost in it. Plan your decision!
In programming, it is not necessary to "go ahead". Give your brain time to analyze and process information.
To get a good plan, answer the simple question:
“By inputting at X, what steps need to be done to get Y at the output?”
Note: programmers have a great tool for this. Comments!
This is the most important step. Be careful!
Do not try to solve one big problem.
Instead, divide it into subtasks. It will be much easier to solve them.
Then solve the subtasks, starting with the simplest. When a task seems simple, it means that you know the answer (or are close to it).
Solve each subtask independently of the others and, after solving, combine the results.
Connecting all the small tasks will give you a solution to the original.
This method is the cornerstone of problem solving. Remember this (reread this step if necessary).
If I could teach every young programmer how to solve problems, it would reduce the amount of technical debt.
Suppose you need to write a program that takes 10 digits and returns the third largest. For a beginner, this can be a difficult task, although it only requires knowledge of the basic syntax.
If you are stuck, you need to simplify. Instead of the third largest number, how about finding the biggest one? Still too hard? How to find the largest of the three numbers? Or more of the two?
Reduce the task until you understand how to solve it. Write down the solution. Then deploy the task until you return to the initial question.
- V. Anton Spraul
Now you are probably sitting and thinking: “Hey, this is all cool, but what if I can't solve this problem?”.
First take a deep breath. Do not worry. It happens to everyone!
The only difference is that the best programmers have fixed bugs and solved problems with interest, and not with irritation.
In fact, here are three things you should try to do when faced with difficulties:
- Debugging Check step by step, where you could make a mistake in your decision. Programmers call this debugging.
“The debugging art is to find out the difference between what you wrote in the program and what you wanted to write”
- Andrew Singer
- Change of approach. Go back a step. Look at the task from a different angle. Is it possible to abstract from the implementation and apply a more general approach?
“Sometimes we go into details so much that we do not take into account the general principles, with the help of which we can solve the problem at a higher level. [...]
A classic example of this, of course, is the summation of a long list of consecutive integers 1 + 2 + 3 + ... + n, which the young Gauss easily calculated using the formula n (n + 1) / 2, avoiding the problems associated with an increase in the number of elements "
- C. Jordan Ball
Note: Sometimes it is better to remove everything and start over with new powers. I'm serious. You will be surprised at how effective this can be.
- Study. Oh, good old Google . No matter what your task, most likely someone has already solved it before you. Find this person or solution. Do it even if you figure it out yourself. You can learn a lot from other people.
Caution: Do not look for a solution to a big problem. Look for solutions only for small subtasks. Why? If you do not strain (at least a little), you will not learn anything new. If you have not learned anything, then you have wasted time.
Do not hope to become a professional in a week. In order to solve problems well, you need to solve many problems!
Practice. Practice. And once again practice. Only in time can you say: “This problem can be easily solved with the help of <substitute your solution here>”.
How to train? There are interesting options!
Chess puzzles, math problems, sudoku, go, monopoly, video games, etc.
In fact, a common practice among successful people is their habit of “solving microtasks”. For example, Peter Thiel plays chess, and Ilon Musk plays video games.
Byron Reeves said: “If you want to see what business leaders look like in three to five years, look in online games.”
Fast forward. Ilon Musk, Reed Hoffman, Mark Zuckerberg and many others say that the games were fundamental to their success in creating their companies.
- Mary Meeker ( 2017 internet trends report )
Does this mean you should only play games? Of course not.
But what is the essence of most games? That's right, in solving problems!
So what should be in practical classes. Something that will allow you to solve many micro tasks (and ideally you like it).
For example, I like programming tasks. And every day I try to solve at least one (mainly on Coderbyte ).
As I said, all tasks have similar solution models.
Now you know what it means to think like a programmer.
You also know that problem solving is an incredible (basic) skill that needs to be developed.
Pay attention, now you also know how to practice your problem solving skills!
Finally, I want you to face many challenges.
“When you think that you have successfully overcome one obstacle, another appears. But that is what makes life interesting. [...]
Life is a process of overcoming obstacles - fortifications through which we must break.
Each time you learn something new.
Each time you will develop strength, wisdom and perspectives.
Each time there will be less and less competitors. And in the end, only your upgraded version will remain. ”
- Ryan Holiday ( The Obstacle is the Way )
Now go solve the problem!
And may luck be with you!
Special thanks to C. Jordan Ball and V. Anton Spraul. For useful advice that they gave.
In addition, I could not acquire my programming knowledge so quickly without Lambda School . I can not thank and do not recommend them.