Problem Solving
Summary/Overview
How to solve problems is a learned skill. With repeated exposure to problems and from repetition of trying to solve them problem solving will become easier. Until then it is sheer will and perseverance and not getting frustrated that will help you reach your goals.
Lesson Goals:
Create A Plan
Breaking Down A Problem
Base case
Useful Strategies
Create A plan
Always have a plan. This is one of the most important rules. Do not engage in random trial and error. Trial and error is fine but random trial and error is not. If you have not yet solved the problem in your head you cannot possibly solve it in code. Your plan may alter or even become useless but it is an important first step to tackling any problem
Tips on creating a plan
Read the problem multiple times
Restate the problem - approach it by working backwards or from a different starting point
List the constraints and if stuck loosen one
Draw out what needs to happen
Write pseudo code
Focus on small approachable steps
Breaking Down A Problem
I cannot stress this enough. This is a common problem students run into. They try to solve the problem in one complete step. Problems become much easier with time and exposure to coding problems. Computers are literal and need detailed precise instruction or they fail. For example to compare a word computers cannot simply evaluate the entire word as whole but each letter is evaluated step by step one at a time.
Breakdown your algorithm into as small of steps as you can
For example Let’s solve this problem if a string is a palindrome ( a word that is the same backwards and forwards ie: madam)
Palindrome Problem
Trying to compare two words as a whole seems easy but computers need a direct step by step instructions aka an algorithm to solve this.
We broke our problem down into 4 parts now but still Step 2 is too large. Let’s break down step 2 into its own group of problems.
Now we have solved step 2 from our original plan let’s put it together
What is a base case?
Many times in programming a problem has a one key condition to our whole problem and the rest is just iterating over the rest of the problem repeating the solution.
What was the base case in our palindrome function?
The last step was our base case. Appending the last character of the word to our reverseWord variable and repeating this process over and over. Commonly your base case will be inside a loop.
Solved?
Once we have solved a problem it is important to test it with different inputs. In this case what happens if we inserted numbers, or capitalized letters? Our code will break if we do not handle these situations. So it is important to handle edge cases after finding a base solution. It is very common to solve a problem then refactor your code to be more robust and efficient.
Key Point: You will not have perfect solution on the first attempt or even second. Do not let perfection be the enemy of progress.
Recap
We started with a large problem broke it down into much smaller steps. Then looked at those steps and restated step 2 of our problem into even smaller problems that we know how to do. This is critical to creating a plan of attack to solving our problems. Sometimes just getting started on what you know will jumpstart the task at hand. Other strategies involve reducing the constraints, making the problem a little easier, maybe you can solve a simpler version of the problem or different part of the problem. Creating analogies to past problems you have solved, look for similarity. Lastly experiment but experiment with a purpose console.log each part of what you expect, always confirming your beliefs. Remember we are dealing with a flow of data and the computer does each thing step by step, line by line and we can capture those steps with console.logs
All of these strategies help your mind see the problems from different angles and break through. So many times in history you hear the solution just came to someone, but it is because they had an approach and kept trying to view the problem from different angles.
Another tip if stuck is to take a break, work on another problem so your mind can process it. Many times I have come back to the problem and it was easier to solve. My favorite trick though is when I have been stuck on something hard I make sure I am thinking about it as I fall asleep. Your mind is more open to different connections in this half sleep dream state and can view the problem from different angles much easier. Many famous inventors would fall asleep thinking about a problem while holding something in their hand. Only to drop it when they fell asleep to leverage this state of open mindedness.
Key Concepts:
Do not get frustrated
Create a Plan and pseudo code it
If a it isn’t solved in our mind then we cannot code it.
Break down the problem, sometimes multiple times
Solve what we know
Reduce complexity, lighten constraints
Take a Break when stuck for long periods of time
Sleep on it
Reference and Optional Reading
Last updated