Find top-k frequent elements efficiently
AppleSoftware Engineer · Technical ScreenLast updated
This question evaluates algorithmic and data-structure skills, focusing on frequency counting, heap-based selection, streaming updates, and time/space complexity analysis within the Coding & Algorithms domain.
Given an array (or stream) of integers and an integer k, return any ordering of the k most frequent values. Implement a solution using a heap and discuss how you would handle ties, negative numbers, very large n, and streaming updates under memory limits. Analyze time and space complexity and outline an alternative bucket-based approach.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.