Category: Heaps & Priority Queues

Heap and priority queue interview problems in Python: top K, merge K sorted lists and running medians, with tested code and complexity analysis.

A heap lets you repeatedly take the smallest or largest item from a changing collection without sorting it again every time. In interviews that shows up as top K problems, merging several sorted lists and keeping a running median. Python makes this easy in one way and awkward in another: the heapq module provides only a min-heap, so problems that need a max-heap call for a workaround. The guide in this category covers size-k heaps, top K frequent elements, K closest points, merging k sorted lists and the two-heap approach to a running median, with the time and space cost of each. Read it when a problem says k largest, k closest or next smallest, because those phrases very often point to a heap.