14 MARCH 2022/LEETCODE

How to solve a Leetcode problem

Overview

Overall, you will find that many leetcode problems are similar to one another. Once you build a solid foundation of knowledge (Blind 75) you will be able to conquer any problem! Keep this in mind when you are looking for patterns within problems. If a string problem stumps you, try thinking of it from an array perspective and vice versa! When I think about Leetcode solutions, I take the following steps:

The process

  1. Clarify the question with your interviewer. ASK QUESTIONS! Don't rush into it.
  2. Understand the problem. Know WHY the answers are correct vs incorrect.
  3. Approach a Brute Force solution, in THEORY but do not code yet.
  4. Write some pseudocode for your brute force solution.
  5. Run through test cases, on your pseudocode to make sure it works. (Time permitting)
  6. Identify bottlenecks, and optimize as necessary.
  7. Code the final solution.
  8. TEST!

Testing

When testing our solutions, it's always good to think about edge cases to test against. This shows our interviewer our deep knowledge of the underlying data structure, as well as the ability to think outside of the box. Here is my (not so) exhaustive list of testing criteria for arrays and strings.

Arrays
  1. Nil input value
  2. Empty array
  3. Single value in array
  4. Repeating values in the array
  5. Large array
Strings
  1. Nil input value
  2. Empty string
  3. Special characters in strings (ie. $%^&#😎😃)
Alex Stevens

Alex Stevens

iOS Software Engineer