You’ve practiced two patterns for data that isn’t a simple list:
- Graphs and grids connect things in any shape, cycles included. You stored neighbors in a
Map, kept aSetof visited nodes so every node was handled once, walked depth-first when the question was about reaching something, and walked level by level when it was about the fewest steps. To spot a circular import, you also tracked which modules were still on the current path. - Heaps keep the smallest item at the top of a tree stored in an array, at O(log n) per push or pop. You built a
MinHeapbecause JavaScript doesn’t ship one, then kept a heap of size k to find the top k items and used it to merge many sorted logs into one.
Things to Remember in an Interview
- Ask whether edges are one-way or mutual, and store a mutual edge in both directions.
- Build the adjacency list in a
Map, not a plain object, and use string or number keys for grid cells. - Mark a node as visited when it joins the queue, and move through the queue with a head index instead of
shift(). - Ask how big and how deep the input can get before you write a recursive walk.
- Write the comparator before the heap code, and say what happens on ties. A heap isn’t stable unless you make it so.
- When you only need the top k, say out loud why you don’t need to sort everything.