0) Mental model (what you reach for)
- Arrays/strings: two pointers, sliding window, prefix sums, sorting + scan
- Hashing: dict/set for O(1) average lookups
- Stacks/queues: parentheses, next greater, BFS
- Trees/graphs: DFS/BFS, recursion/iterative, visited sets
- Heaps: “top k”, streaming min/max
- DP: 1D/2D transitions, memoization
- Binary search: monotonic predicate, lower/upper bounds
- Greedy: sort by key, take best local choice
- Intervals: sort by start, merge/scan with end pointer
1) Core syntax you should be fluent in
Variables, loops, conditionals
x = 0
if x > 0:
pass
elif x == 0:
pass
else:
pass
for i in range(n): # 0..n-1
pass
for i in range(a, b, step): # a..b-1
pass
while condition:
pass
Functions (including helpers)
def f(a: int, b: int) -> int:
return a + b
def g(nums):
def helper(i):
return i + 1
return helper(0)
Useful Python idioms
# swap
a, b = b, a
# reverse
nums.reverse()
s = s[::-1]
# min/max with key
m = min(items, key=lambda x: x[1])
# enumerate
for i, v in enumerate(nums):
pass
# zip
for a, b in zip(A, B):
pass
2) Built-in data structures + common operations