Top K Video View Counts
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The trap in Google's Top K Video View Counts, reported in October 2026, is k bigger than the array. The statement says return min(k, viewCounts.length) values, and a naive solution that indexes k items crashes or pads with garbage. The task is simple: take the largest view counts and return them from biggest to smallest. A heap is the pattern the problem points to, though sorting also passes. If you're taking this OA in the next day or two, read the edge cases first. StealthCoder sits behind the live assessment as a safety net if your mind goes blank on the details.
The problem
A short-video service stores one nonnegative view count for each video. Given the array viewCounts and an integer k, return the min(k, viewCounts.length) largest view counts in nonincreasing order. Examples Example 1 viewCounts = [120,50,300,200,80] k = 3 return = [300,200,120] The three largest view counts are 300, 200, and 120, listed from largest to smallest.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Two clean routes. First, sort descending and slice the first min(k, n) items. That's O(n log n) and hard to get wrong. Second, keep a min-heap of size k, push each count, pop the smallest when the heap exceeds k, then sort the survivors descending. That's O(n log k), and it's what an interviewer expects when they say heap. The pitfalls are all edges. k larger than n, k equal to 0, an empty array, and duplicate counts like [5,5,5], where you keep duplicates because each is a separate video. Don't dedupe. Also remember the output order is nonincreasing, and a heap's pop order is the reverse, so you need to reverse it. If you blank on heap syntax in your language during the live OA, StealthCoder can supply a working version while you check it against the edge cases.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Top K Video View Counts cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Google's OA.
Google reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Top K Video View Counts FAQ
How hard is Top K Video View Counts really?+
Easy to medium. The core is a standard top-K selection. The difficulty is in the details: k exceeding the array length, zero counts, duplicates, and returning results in nonincreasing order. If you handle those four, you pass.
What's the trick to solving it?+
Use a min-heap capped at size k. Push each view count, and pop the smallest whenever the size passes k. What's left is your top k. Then sort descending for the output. Cap k at the array length first so nothing breaks.
Can I just sort the array instead of using a heap?+
Yes. Sort descending and take the first min(k, n) elements. It's O(n log n), which is fine unless the constraints are huge. A heap is O(n log k) and shows more intent, but correct sorting beats a buggy heap every time.
What edge cases should I test before submitting?+
Test k greater than the length, k equal to the length, k equal to 0, an empty array, a single element, and duplicates such as [5,5,5] with k=2. Duplicates must stay in the result. Also confirm the output is descending, not heap order.
How do I prepare for this in 48 hours?+
Write top-K with a heap once in your OA language and learn its quirks. Python's heapq is a min-heap by default, while Java's PriorityQueue needs a comparator for max behavior. Then write the sort version as a backup. Run the edge cases above against both.