How to Solve Any Problem

You'll learn a lot but what do you do when you come across a problem you don't know how to solve?

You're going to learn four steps to solve any problem. You read that right: you can apply these steps to ANY problem.

  1. Understand the problem
  2. Make a plan
  3. Carry out the plan
  4. Look back and improve your solution

Credit to mathematician George Polya for coming up with these steps.

1. Understand the problem

Seems obvious, right? Don't take this step lightly! This is the most important and usually the most difficult step. If you understand the problem, the plan becomes obvious. If you have a solid plan, the code is easy to write. First, you must understand the problem.

Let's say you're in a job interview and are given the following problem:

    Given a string, write a function to return true if the string's last character
    is either an 'E' or a 'e'.

    Input: "stance" Output: true
    Input: "FLOAT"  Output: false

    Constraints:
    * Use a maximum of one comparison operator in the function code block.
    

You have a limited amount of time so you may be tempted to start coding as soon as possible. Resist the temptation! Take a few minutes, read over the problem to make sure you understand.

You should be speaking these thoughts out-loud like you would to your interviewer.

Sometimes interviewers purposely leave out information or constraints required to solve the problem. Your job is to pick up on it and ask them. Interviewers are testing you on how you would work in a team to get the coding task done.

In this particular example, there is an obvious input and output that should be tested for but wasn't listed in the example inputs and outputs. Can you figure out what that is?

2. Make a plan

Once you have a good understanding of the problem, you may again be tempted to start writing code. Hang on! A few minutes spent planning can save you hours of coding down the wrong path.

In computer science, like any science, it's perfectly reasonable to come up with a hypothesis (guess) then test it out (check). Get creative!

In interviews, communication is key. It's never too early to practice for technical interviews! Get used to sharing your thought process and plan out loud. A good question to ask yourself (or an interviewer) is: "Here's what I'm thinking... Am I on the right track?"

Here's a plan for the example problem:

Tip: As a beginner, you should try to be as detailed as possible with your plan. Even though some logic or step may seem obvious and could seem too tedious or obvious to write down, you should still do so anyway! Beginners usually make the mistake of missing key steps. Try to think like a computer when coming up with the steps! Break the steps down using boolean logic.

Sounds good! Now you can start coding!

3. Carry out the plan

This is the easiest of all the steps but it's also the one where most people get stuck. The reason for that is usually from coding without a plan, or executing a flawed plan because they don't quite understand the problem. If you've put time into steps 1 and 2, step 3 is mostly remembering syntax.

Code comments are a great way to state your plan within your code. You can use the comments as a skeleton or outline of what you want to code. This is called pseudocode.

// 1. Define a function with the name `checkIfLastCharIsAnE`.
// 2. Define a single parameter for the function called `str`.
// 3. Find the last character of `str`.
// 4. Return true if the last character of `str` is an 'e' or if the last
//    character of `str` is an 'E'. Otherwise return false.
    

From here, execute your plan step by step in your code. Now, you just have to translate the comments into code.

// 1. Define a function with the name `checkIfLastCharIsAnE`.
function checkIfLastCharIsAnE() {

}
// 2. Define a single parameter for the function called `str`.
// 3. Find the last character of `str`.
// 4. Return true if the last character of `str` is an 'e' or if the last
//    character of `str` is an 'E'. Otherwise return false.
function checkIfLastCharIsAnE(str) {
    // 3. Find the last character of `str`.
    let lastChar = str[str.length - 1];
    // 4. Return true if the last character of `str` is an 'e' or if the last
    //    character of `str` is an 'E'. Otherwise return false.
    return lastChar === 'e' || lastChar === 'E';
}
    

4. Look back and improve your solution

First, test your code to make sure it works with the given example inputs and outputs. Then test your code against inputs and outputs that you came up with yourself.

If you run this solution, you should get this:

checkIfLastCharIsAnE('stance'); //=> true
checkIfLastCharIsAnE('FLOAT');  //=> false
checkIfLastCharIsAnE('PHONE');  //=> true
    

Once you've come up with a solution that seems to work with those inputs and outputs, it's always good to revisit your code and make improvements:

The code looks great! ...or does it? Upon closer inspection, you might realize that the code violates the constraint of "Use a maximum of one comparison operator in the function code block."

To solve this problem, restart the steps in the Polya framework.

Stuck? Take a step back

If you ever run into trouble while problem solving, take a step back. So, if your code isn't working, step back and re-examine your plan. Outline the plan in comments or pseudocode. If you're having trouble coming up with a plan, take a step back and make sure you really understand the problem. Ask questions if you need to. Most people will get stuck on Step 3 but the problem usually lies in Step 1 or 2.

Keep in mind that if you've put the time into steps 1 and 2, step 3 should be easy.

This framework also works in any language. Here's the exact same plan, executed in Python:

# 1. Define a function with the name `check_if_last_char_is_an_e`.
# 2. Define a single parameter for the function called `str`.
def check_if_last_char_is_an_e(str):
  # 3. Find the last character of `str`.
  last_char = str[-1]
  # 4. Transform the last character of `str` to a lowercase character
  last_char = last_char.lower()
  # 5. Return True if the last character of `str` is an 'e'. Otherwise return False.
  return last_char == 'e'

check_if_last_char_is_an_e('stance') #=> True
check_if_last_char_is_an_e('FLOAT')  #=> False
check_if_last_char_is_an_e('PHONE')  #=> True
    

What you've learned

You've learned 4 steps to solve any problem. Use them wisely!

  1. Understand the problem
  2. Make a plan
  3. Carry out the plan
  4. Look back and improve your solution