Data structures & algorithms
Curated patterns for Python coding interviews. Each file covers one pattern with explanation + 3-5 canonical problems, with Python-idiomatic solutions.
New here? Start with 00_data_structures.md — a reference of the data structures themselves (main features, operation costs, and the Python type for each). The sections below are algorithmic patterns that operate on those structures.
Sections
- 01_arrays_strings/ — two pointers, sliding window, prefix sums
- 02_hashmaps_sets/ — counting, top-K, dedup
- 03_linked_lists/ — reverse, cycle detection, merge
- 04_trees/ — DFS/BFS, BST validation, LCA, serialization
- 05_graphs/ — BFS, DFS, topological sort, union-find, shortest path
- 06_dp/ — memoization, tabulation, classic problems
- 07_recursion_backtracking/ — permutations, subsets, N-queens
- 08_sorting_searching/ — binary search variants, quickselect
- 09_python_specific/ —
bisect,heapq,deque, idiomatic patterns
How to use this
Don’t memorize solutions. Memorize the pattern and recognize when it applies. A coding interview is mostly about identifying which pattern fits the problem in 30 seconds, then implementing it cleanly.
Suggested practice approach:
- Read the pattern explanation.
- Try the first problem on paper or whiteboard, no IDE.
- Compare with the solution. If you got it: move on. If not: redo it from scratch tomorrow.
- After all patterns, do mixed practice (LeetCode “Top 75” or NeetCode 150).
Big-O quick reference
| Structure | Access | Search | Insert | Delete |
|---|---|---|---|---|
array (list) |
O(1) | O(n) | O(1) end / O(n) middle | O(n) |
| linked list | O(n) | O(n) | O(1) | O(1) given node |
hash map (dict) |
— | O(1) avg | O(1) avg | O(1) avg |
| BST (balanced) | O(log n) | O(log n) | O(log n) | O(log n) |
| heap | — | O(n) | O(log n) | O(log n) min/max |
deque |
O(1) ends | O(n) | O(1) ends | O(1) ends |
| Algorithm | Time | Space |
|---|---|---|
| Linear search | O(n) | O(1) |
| Binary search | O(log n) | O(1) |
| Quicksort (avg) | O(n log n) | O(log n) |
| Mergesort | O(n log n) | O(n) |
| BFS / DFS | O(V+E) | O(V) |
| Dijkstra (heap) | O((V+E) log V) | O(V) |
Added patterns
| Folder | File | Pattern |
|---|---|---|
10_stacks_queues/ |
10_stacks_queues/01_monotonic_stack.md | monotonic stack and deque — turns O(n^2) into O(n) |
11_intervals/ |
11_intervals/01_merge_and_sweep.md | merge, overlap, sweep line — and which key to sort by |
06_dp/ |
06_dp/02_knapsack_lcs_edit_distance.md | 2D DP: knapsack, LCS, edit distance |
04_trees/ |
04_trees/02_bst_and_tries.md | BST invariant and deletion; tries for prefix problems |
12_greedy_and_bits/ |
12_greedy_and_bits/01_greedy_and_bit_manipulation.md | greedy justification, XOR tricks, bitmasks |