DeepOffer

Given Item Counts, How Would You Return the K Most Frequent Values?

ML CodingHot interview question
Reported in public interview compilations — Apple, Amazon, Google, Meta

Count first: Counter for frequencies, O(n).

Select Top K: Maintain a min-heap of size K keyed by frequency - O(n log K) overall. Or sort all unique elements O(m log m), where m is the number of distinct elements. State complexity and tradeoffs proactively.

Faster option: Bucket sort by frequency in O(n). Pull this out when the follow-up pushes for linear time.

Python pitfalls: heapq compares tuples, so make sure ties never reach uncomparable objects. nlargest is a one-liner, but be ready to hand-write the heap version.

Common follow-up questions

Practice this question with an AI interviewer

Get asked follow-ups live, then receive a scored report — like a real MLE interview loop.

Start AI mock interview