Skip to content

Lessons

Abstract

What came to my mind while solving the 48 problems in LeetCode Top Easy.

2026-04-07

  • Read the questions and conditions carefully.
  • Find the solution on paper. If you don't find it on paper, odds are you won't implement it correctly on a computer.
  • Find the trick. There's always one. When you find it, the solution becomes simple and classic.

2026-04-11

  • Draw.
    • Try different visual representations.
    • They help you see and find patterns.
    • They help you think about the right data structures to use.
  • Run the algorithm by hand with visuals and small values.
  • Re-state or reformulate the problem.

2026-04-14

  • Check your tests. When they fail, maybe some test cases are wrong and your implementation is right.
  • Check your helpers. Make sure the helpers you use in your tests to build data structures like lists, trees, ... are correct. Your tests may be failing because those helpers are wrong.

2026-04-15

  • Group or isolate elements. When you're reasoning about a "set" of something—it can be a tuple, list, set, tree, ...—change the way you look at it. You can look at its elements one by one, or see them as subsets. For instance, if x = [1, 2, 3, 4, 5], you can look at 1, or any other element, or look at it like this: x = [A | B] where A = [1, 2] and B = [3, 4, 5]. See problem 189. Rotate Array for instance.

2026-04-19

  • Learn the trick. Sometimes the solution, especially because of time and space constraints, uses a property of the system or a specific algorithm you've never seen before. Chances are you won't find it in limited time. That's ok. Don't take it personally. After trying your best, just learn the trick and add it to your toolkit. For instance, in 136. Single Number, you can get a program with \(O(1)\) space by keeping in mind the equalities a ^ a = 0 and a ^ 0 = a for any integer a, and the fact that XOR is commutative and associative.
  • Never forget you're doing an interview. A solution for an interview must please the interviewer and show what the interviewer expects, not the most idiomatic solution. For example, in 125. Valid Palindrome, don't use a regex pattern match on a string when a two-pointer solution that traverses the string as an array is expected.
  • Never rush or try to go fast. Find the flow. You'll be at your maximum speed, and the problem will solve itself.

2026-04-20

  • Take advantage of the constraints. Most of the time, constraints affect the program at the edges: empty list, starting with at least one node in a tree, ruling out negative values, ... But sometimes, they let you choose a totally different approach or algorithm. See problem 326. Power of Three for instance, where testing whether \(n\) is a power of \(3\) becomes testing whether \(n\) divides \(3^{19}\), the largest power of \(3\) smaller than \(2^{31** - 1\), the upper bound of the constraint.

2025-04-25

  • Recognize the pattern. It's only possible to do this after you've seen a pattern at least once. Practice more, and you'll have more patterns in your toolkit. Simple.