Skip to content

Lessons

Abstract

What came to my mind while solving random LeetCode questions.

2026-05-26

  • Use a stack. If you need to process in reverse order of consumption, use a stack. See 445. Add Two Numbers II.
  • Use a queue. If you need to process in same order of consumption, use a queue.

2026-05-27

  • Think fast. Fail fast. And solve the problem.
  • Don't fall into the trap. Sometimes problems are not what they seem to be. For instance, 290. Word Pattern looks like a simple string parsing, but is in fact a small hash map problem. Likewise, 542. 01 Matrix looks like DP, but the clean solution is really a graph shortest path from many sources at once.
  • Reverse the search. For instance, in 542. 01 Matrix, the problem is framed like this "return the distance of the nearest 0 for each cell". But the right way to look at it is to reverse the search and start from 0 nodes and expand one layer at a time until reaching 1s of non processed nodes yet, which gives the shortest distances.

2026-05-28

  • Don't confuse linear time with one pass. 2 passes is still linear time: \(O(2n) = O(n)\). See 229. Majority Element II.

2026-06-01

  • Write simple and correct code fast. Sometimes this means using built-ins like in 929. Unique Email Adresses where using split and replace string methods makes the code easy to follow. Remember that in interviews, "better" usually means:
    • correct
    • easy to explain
    • easy to verify
    • hard to break
    • written quickly

2026-06-02

  • Don't forget to advance while loops. For instance:
    • while i < n: ... expects i += 1 in while block.
    • while left < right: ... expects left += 1 or right -= 1 or both in the while block.
    • while stack: ... expects stack.pop() in the while block.
    • while queue: ... expects queue.popleft() in the while block.
    • while tail: ... expects tail = tail.next in the while block, assuming tail has the property next to advance the list.