Blog
Notes on data structures & algorithms, system design, and things I learn along the way.
Learning DSA? Follow the structured path — ordered articles from recursion to dynamic programming.→A grid is a graph: each cell is a node, and its neighbours are its edges. How flood fill (the paint-bucket tool) and 'number of islands' are BFS or DFS over that implicit graph, how to avoid stack overflows and revisits, and the family of grid problems the same template solves.
Give frequent symbols short codes and rare ones long codes. How Huffman's algorithm builds an optimal prefix code by repeatedly merging the two least frequent symbols with a min-heap, why no code is a prefix of another, and where Huffman coding still runs today — inside gzip, PNG and JPEG.
Find the longest substring that reads the same forwards and backwards. Why checking every substring is O(n³), how expanding around each of the 2n − 1 centres gives a simple O(n²) solution with O(1) memory, the DP alternative, and where Manacher's linear-time algorithm fits.
How much can flow through a network of pipes with limited capacities? How augmenting paths found by BFS push flow until none remain, why residual edges let the algorithm undo earlier choices, how the max-flow equals the min-cut, and the matching and assignment problems that reduce to it.
Turn every interval into a start event and an end event, sort them, and walk through with a running count. How the sweep-line technique answers 'how many meeting rooms?', 'when was the system busiest?' and the skyline problem in O(n log n), and the tie-breaking rule that decides correctness.
Can a graph's nodes be split into two groups so every edge crosses between them? How a BFS that alternates two colours answers it in linear time, why the answer is 'no' exactly when there's an odd cycle, and the problems that are secretly this one — from scheduling to splitting people into teams.
When a DP state has to remember which items have been used, a bitmask can encode the set in one integer. How that turns the travelling salesman problem from n! routes into 2ⁿ·n states, the bit tricks you need, and how to recognise problems where n ≤ 20 is a hint.
Merging k sorted lists into one by repeatedly taking the smallest head — with a min-heap so each step costs O(log k). The pattern behind merge-sort's final pass, external sorting of files bigger than memory, LSM-tree compaction, and a family of interview problems.
The longest sequence of items two lists share in order, not necessarily side by side. How a small DP table finds it, how walking back through the table recovers it, and how the same idea — applied to lines instead of letters — produces the diffs you read in every code review.
The deepest node that is an ancestor of two given nodes in a tree. The simple walk-up solution, then binary lifting: precompute every node's 1st, 2nd, 4th, 8th… ancestor so any query takes O(log n) jumps. With distances between nodes and the k-th ancestor as bonuses.
51 posts tagged DSA · page 1 of 6