Code Room
Code reviewMedium
Question
Review this Python code that keeps the top-N items by score using a heap.
What a strong answer looks like
Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix — specific and kind.
Learn the concepts
import heapq class Item: def __init__(self, score, payload): self.score = score self.payload = payload def top_n(items, n): heap = [] for it in items: heapq.heappush(heap, (it.score, it)) if len(heap) > n: heapq.heappop(heap) return [pair[1] for pair in heap]Run or narrate your approach, then ask the coach.