I bought the course JavaScript Algorithms and Data Structures Masterclass a while ago because I feared that I needed to do algorithm challenges when applying for a job. Luckily, that was not the case.

It’s still a useful resource and I liked the tips for solving problems.

The tips come from the book How to Solve It by George Polya. It’s a book on mathematics, but the principles apply to programming, too.

1. Understanding the Problem

  1. Can I restate the problem in my own words?
  2. What are the inputs that go into the problem?
  3. What are the outputs that should come from the solution to the problem?
  4. Can the outputs be determined from the inputs? Do I have enough information to solve the problem?
  5. How should I label the important pieces of data that are a part of the problem?

2. Concrete Examples

Coming up with examples can help you understand the problem better. Examples provide sanity checks: user stories and unit tests.

  • start with simple examples
  • progress to more complex examples
  • examples with empty inputs
  • examples with invalid inputs

3. Break It Down

Use pseudo-code or code comments. In an interview, explain the steps you want to take without worrying about details like language syntax.

4. Solve the Problem (or Solve a Simpler Problem)

If you can’t solve the complete problem, try to solve a simplified version.

5. Look Back and Refactor

  • Can you check the result (does it work)?
  • Can you derive the result differently?
  • Can you understand it at a glance?
  • Can you use the result or method for some other problem?
  • Can you improve the performance of your solution?
  • Can you think of other ways to refactor?
  • How have other people solved this problem?