Heap and priority-queue coding interview questions 8 questions in 1 chapter

Eight heap and ranking coding questions in Java and Python, from kth-largest and top-k words to medians, merges and scheduling.

Part of Applied AI Engineer (LLM) coding learning path

  1. 01Top-k and streaming8 questions

Top-k and streaming

A size-k heap, a stream version, and a two-heap running median.

  • 1.1 Kth Largest Element in an Array Medium 3 min

    I keep a min-heap of size k and scan the array. Every value is offered to the heap, and if the heap grows past k I drop the smallest, so the heap root is the kth largest. That is n log k time. Quickselect is linear on average if I am allowed to mutate the array.

    Read the full answer
  • 1.2 Kth Largest Element in a Stream Easy 3 min

    I seed a min-heap with the starting numbers and then keep only k values in it. Each add offers the new number and drops the smallest if the heap is larger than k, so the root is the current kth largest.

    Read the full answer
  • 1.3 Find Median from Data Stream Hard 4 min

    I split the stream into two heaps: a max-heap of the lower half and a min-heap of the upper half. The heaps are kept the same size, or the lower half one element larger. The median is then the lower root, or the average of the two roots when the count is even.

    Read the full answer
  • 1.4 Top K Frequent Words Medium 4 min

    I count word frequencies, then I keep a min-heap of size k that is ordered by frequency, with a reverse lexical tie-break so the heap root is the weakest candidate. After the scan I pop the heap and reverse the list, which yields the k words in the required order.

    Read the full answer
  • 1.5 Merge k Sorted Lists Hard 4 min

    I put the head of each non-empty list into a min-heap ordered by node value. I repeatedly pop the smallest node, append it to the result, and if that node has a next pointer I push that next node. With k lists and n nodes overall, that is n log k time.

    Read the full answer
  • 1.6 Find K Pairs with Smallest Sums Medium 5 min

    Both arrays are sorted, so the smallest pair starts at the two first elements. I put that pair in a min-heap of sums, then repeatedly pop the smallest pair and push the next unvisited neighbour in the second array, and the next neighbour in the first array when the second index is zero, so I do not explore the whole n by m grid.

    Read the full answer
  • 1.7 Reorganize String Medium 4 min

    I count character frequencies and fail early if one character would have to occupy adjacent slots, that is, if its count is more than (n+1)/2. Then I always take the most frequent remaining character that is not the one I just placed, using a max-heap of counts, and I push the previous character back if it still has a leftover count.

    Read the full answer
  • 1.8 Task Scheduler Medium 5 min

    The idle time is forced by the most frequent task and the cool-down n. I compute (maxCount - 1) times (n + 1), then add how many tasks share that maximum count. If that formula is smaller than the number of tasks, the answer is just the length of the task list, because the cool-down is already absorbed by other work.

    Read the full answer