Lessons
Abstract
What came to my mind while solving:
- LeetCode Top Easy (48 problems).
- Random LeetCode problems (17 problems - Google Interview Prep).
- LeetCode Top Medium (45 problems).
- LeetCode SQL 50 (50 problems).
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 at1, or any other element, or look at it like this:x = [A | B]whereA = [1, 2]andB = [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 = 0anda ^ 0 = afor any integera, 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.
2026-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.
2026-07-02
- Start sentences with precise verbs when describing algorithms.
For instance:
- "Scan left to right and compare each symbol with the next one." from 13. Roman to Integer.
- "Keep the current minimum alongside each pushed value." from 155. Min Stack
- "Write from the end." from 88. Merge Sorted Array
Random LeetCode problems (Google Interview Prep)¶
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
splitandreplacestring 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: ...expectsi += 1inwhileblock.while left < right: ...expectsleft += 1orright -= 1or both in thewhileblock.while stack: ...expectsstack.pop()in thewhileblock.while queue: ...expectsqueue.popleft()in thewhileblock.while tail: ...expectstail = tail.nextin thewhileblock, assumingtailhas the propertynextto advance the list.
LeetCode Top Medium¶
2026-06-07
- Find a canonical representation. When you're dealing with a
class of objects defined by some constraints, look for a canonical
representation of that class, preferably one that's hashable.
- For instance, all anagrams have the same sorted-letter representation, and so we can use it as a key to group strings by anagrams. See 49. Group Anagrams.
- Another example: in 36. Valid Sudoku we pick
(r // 3) * 3 + (c // 3)as the box id, a canonical way to represent belonging to one of the 3x3 boxes in a Sudoku grid.
2026-06-08
-
Store the index, not only the value. When you find an element in a Python list (array), you have its index, its positions. So you can move a pointer to this position in \(O(1)\) later without rescanning the array. You must take advantage of this property of Python list. For instance, in 3. Longest Substring Without Repeating Characters, storing the indices along the characters of the characters you scan in the list lets you move the left pointer of the sliding window in \(O(1)\).
-
Build a list of parts and join them to make a string. When iteratively building a string, prefer to build a list of the parts and join them after the loop is finished. It's more efficient than concatenating strings while iterating. See [38. Count and Say](leetcode_top_medium_01_array_and_strings_07_count_and_say.md** for instance.
2026-06-11
- Find the minimal state your program needs to take its next action at any time.
2026-06-12
- Mark visited nodes within the input data. This is \(O(1)\) space
contrary to \(O(n)\) if using a different global set, dictionary,
matrix, etc.
- For instance, in 200. Number of Islands, we can mark visited
cell by sinking land into water like this
grid[r][c] = "0", so that we don't count the same island twice. - For instance, in 79. Word Search, we can mark the cell
board[r][c] = "#"(and storing its value inchvariable) so that we don't consider that cell again for the path we're building. And when we backtrack to build another path, we restore its valueboard[r][c] = chto make it available for the new path.
- For instance, in 200. Number of Islands, we can mark visited
cell by sinking land into water like this
2026-06-23
- Don't enumerate. In LeetCode problems, only in rare cases you'll be asked to enumerate explicitly. But if you think about path enumaration, tuple enumaration, etc. to solve the problem, you're almost always going in the wrong direction. There's is an invariant about the problem, an approach you haven't seen yet. Find it. Enumeration has really bad time complexity. For instance in 55. Jump Game, exploring every possible paths to find one that reaches the end of the array is useless and time expensive. Keeping the maximum reachable index at each step while scanning the array is enough.
2026-06-25
- Don't be lazy. When you have an idea for solving part of the
problem: 1) validate it or 2) discard it. Never leave it
unclassified just because you're too lazy to give it a shot. For
instance, while trying to solve 380. Insert Delete GetRandom O(1):
- I had the idea that I would need a list of the values, and a map from value to index, for the GetRandom operation. But I didn't really try to see how it could be implemented.
- While implementing a solution after reading AI comments, I saw that I had to remove values from the list in \(O(1**\). But I didn't think enough to have a chance to find the simple solution: swap with the last value in the list, then pop the list.
2026-07-17
- Generate permutations in-place. When dealing with permutations, for each index, swap it with any element in the rest of the list (including at itself). See 384. Shuffle an Array (random permutations) and 46. Permutations. Remember that the permutation group is generated by all transpositions, that is, swaps between pairs.
LeetCode Top Hard¶
2026-08-02
- Think heap when you need the k min or k max of some series. See 215. Kth Largest Element in an Array and 23. Merge k Sorted Lists.
LeetCode SQL 50¶
2026-07-13
- Manage your energy. If you're tired after solving a problem in SQL, take a break and come back fresh to solve it in Pandas. Don't look up the answer before really trying to solve it yourself. You never learn anything that way.
2026-07-14
- Don't use reserved keywords in SQL queries. For instance, in
1661. Average Time of Process per Machine, I tried to use
Endas a CTE table name, and it errored. When I changed it toEndActivity, it worked fine. This was likely because of the SQL syntaxCASE ... END.