This felt like one question that kept growing arms.
Start by clarifying requirements and edge cases, then present a heap-based solution using a frequency map and a min-heap of size k. Walk through tie-breaking, negative numbers, large inputs, and streaming constraints, analyzing time/space complexity and comparing with a bucket-based alternative.
Pro tip: Emphasize that tie-breaking is arbitrary unless specified, and in streaming scenarios, use a space-saving algorithm like Misra-Gries or count-min sketch for approximate heavy hitters. This shows awareness of real-world constraints and trade-offs.
Ask about input size, memory limits, whether ties matter, and if the stream is static or dynamic. Confirm expected output format (any valid ordering).
Build a frequency map, then use a min-heap of size k to keep the k most frequent elements. For streaming, maintain counts and update heap incrementally.
Discuss handling ties (arbitrary or stable), negative numbers (hash map handles them), large inputs (external sorting or distributed counting), and streaming under memory constraints (approximate algorithms).
State time O(n log k) and space O(n) for heap approach. Describe bucket-based alternative: O(n) time and space by bucketing frequencies, but requires knowing max frequency.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.