Python 3.11+
The examples use the standard Python syntax and containers supported by the browser Judge; the Top K invariant is independent of a minor Python release.
Verify in Python docs(opens in a new tab)Keep only the best k candidates with a heap, quickselect, counting, or bucket strategy. Learn its decision rule, maintained state, Python cost model, and transfer from a focused drill to a full Interview Problem.
Recognize it when
Consider Top K when the prompt's constraints and required operations match this shape: Keep only the best k candidates with a heap, quickselect, counting, or bucket strategy.

Sign in to save your code and progress across devices. The lesson and problem statement remain public.
A size-k min-heap summarizes the current winners. Items weaker than its root cannot belong to the final top k, so the algorithm never needs to retain them.
For k largest values, the min-heap root is the weakest current winner. For k smallest, use an inverted max-heap. State this meaning before choosing push, replace, or comparison direction.
import heapq
def top_k_trace(values,k):
heap=[]; trace=[]
for value in values:
if len(heap)<k: heapq.heappush(heap,value)
elif k and value>heap[0]: heapq.heapreplace(heap,value)
trace.append(sorted(heap))
return traceImplement top_k_trace(values, k). Return the sorted min-heap contents after each input value is considered.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
Fill the heap to k, then replace the root only when a new candidate is stronger. Heap replacement costs O(log k), giving O(n log k) time and O(k) working space.
import heapq
def k_largest(values,k):
heap=[]
for value in values:
if len(heap)<k: heapq.heappush(heap,value)
elif k and value>heap[0]: heapq.heapreplace(heap,value)
return sorted(heap,reverse=True)Implement k_largest(values,k). Return the k largest values in descending order without mutating input.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
Use a heap when k is much smaller than n or values stream incrementally. Full sorting is simpler when all ranks are needed or k approaches n. Quickselect can give average O(n) selection but does not directly order the winners.
Say: “The root is the weakest of the current k winners. Anything no better than it can be discarded; a stronger candidate replaces it.” Include k=0, duplicates, output ordering, and the O(n log k) bound.
Python 3.11+
The examples use the standard Python syntax and containers supported by the browser Judge; the Top K invariant is independent of a minor Python release.
Verify in Python docs(opens in a new tab)Interview bounds depend on the stated representation, input model, and real Python operations.
| Operation | Average | Worst | Interview note |
|---|---|---|---|
| Top K decision loop | O(n log k + k log k) | O(n log k + k log k) | Use a size-k max-heap represented by negated values, replacing its largest candidate when a smaller stream value arrives. |
O(k) for the focused Keep the K Smallest Stream Values implementation.
These are the mistakes most likely to survive a happy-path example and fail a boundary case.
Applying the pattern without proving its ordering, monotonicity, or window invariant can silently miss valid candidates.
Prevent it: Write the decision rule beside the loop and verify it against Keep the K Smallest Stream Values before optimizing.
Avoid
def k_smallest_values(values, k):
passUse instead
import heapq
def k_smallest_values(values, k):
if k == 0:
return []
candidates = []
for value in values:
if len(candidates) < k:
heapq.heappush(candidates, -value)
elif value < -candidates[0]:
heapq.heapreplace(candidates, -value)
return sorted(-value for value in candidates)Where you will hit this: Keep the K Smallest Stream Values(opens in a new tab)
Uses a min-heap and ejects the smallest candidate, producing large values instead.
Prevent it: Use the public tests and preserve this state: Heap size never exceeds k.
Avoid
import heapq
def k_smallest_values(values,k):
h=[]
for v in values:
heapq.heappush(h,v)
if len(h)>k: heapq.heappop(h)
return sorted(h)Use instead
import heapq
def k_smallest_values(values, k):
if k == 0:
return []
candidates = []
for value in values:
if len(candidates) < k:
heapq.heappush(candidates, -value)
elif value < -candidates[0]:
heapq.heapreplace(candidates, -value)
return sorted(-value for value in candidates)Where you will hit this: Keep the K Smallest Stream Values(opens in a new tab)
Python slicing, copying, membership in lists, or repeated sorting inside the loop can invalidate the intended complexity.
Prevent it: Count every slice, copy, sort, membership check, and container update before claiming O(n log k + k log k).
Avoid
import heapq
def k_smallest_values(values,k):
h=[]
for v in values:
heapq.heappush(h,v)
if len(h)>k: heapq.heappop(h)
return sorted(h)Use instead
import heapq
def k_smallest_values(values, k):
if k == 0:
return []
candidates = []
for value in values:
if len(candidates) < k:
heapq.heappush(candidates, -value)
elif value < -candidates[0]:
heapq.heapreplace(candidates, -value)
return sorted(-value for value in candidates)Where you will hit this: Kth Largest Element in an Array(opens in a new tab)
Official Python documentation supports language behavior. Canonical problem pages provide additional practice context.
python-docs · checked 2026-07-27