Graphs, heaps and intervals

Graph search on grids and networks, topological ordering, heaps for top-k and running medians, and sorting-based interval problems.

About 5 hours of study

Learning objectives

Lessons

  1. 1

    Graph patterns

    Grids are graphs: counting islands with DFS or BFS, shortest paths in unweighted graphs with BFS, and ordering tasks with topological sort.

  2. 2

    Heaps and intervals

    Top-k problems with a heap (heapq), merging overlapping intervals after sorting, and counting meeting rooms by sweeping start and end times.

Module practice

Module quiz

1. Which search finds the fewest steps in an unweighted graph?
2. When marking cells visited in a flood fill, when should you mark?
3. A topological sort outputs fewer nodes than exist. What does that mean?
4. Which heap holds the k largest numbers seen so far?
5. In merging intervals, why take the max of the two ends?
6. To maintain a running median with two heaps, what do the heaps hold?

‹ Linked lists and treesDynamic programming, backtracking and the mock interview ›