The single-pass constraint is what tripped me up.
Clarify that a single linear scan means O(n) time, so we need a hash map for frequency counting and a bucket sort (array of lists indexed by frequency) to avoid O(n log n) sorting. Then extract the top k by iterating buckets from highest frequency to lowest, and for ties, sort the elements within each bucket by value ascending.
Pro tip: Mention that bucket sort works because frequencies are bounded by n, and that tie-breaking by value can be handled by sorting each bucket or by using a min-heap of size k with a custom comparator. Also note that if k is small, a heap might be more space-efficient, but bucket sort is simpler for O(n) time.
Confirm that 'single linear scan' means O(n) time and that we can use O(n) extra space. Ask about input size, whether k can be larger than the number of distinct elements, and if the output should be a list of elements or frequencies.
Use a hash map to count the frequency of each element in a single pass through the array. This is the only pass over the input array.
Create an array of lists (buckets) where the index represents frequency (from 1 to n). Place each distinct element into the bucket corresponding to its frequency. This avoids sorting by frequency.
Iterate buckets from highest frequency to lowest. Within each bucket, sort elements by value ascending (or use a min-heap of size k with comparator: higher frequency first, then lower value). Collect until k elements are found.
State time complexity: O(n) for counting + O(n) for bucket creation + O(k log k) for sorting within buckets (or O(n log k) with heap). Space: O(n). Discuss edge cases: k=0, k > distinct elements, all elements same frequency.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.