Top K Video View Counts
Updated
Problem statement
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.
Each array entry represents a distinct video, so equal view counts remain separate entries in the result. If k == 0, return an empty array. If k is at least the number of videos, return every view count in nonincreasing order.
Function
topKVideoViews(viewCounts: int[], k: int) → int[]Examples
Example 1
viewCounts = [120,50,300,200,80]k = 3return = [300,200,120]The three largest view counts are 300, 200, and 120, listed from largest to smallest.
Example 2
viewCounts = [9,1,4]k = 0return = []When k == 0, no view counts are selected.
Example 3
viewCounts = [7,7,3]k = 5return = [7,7,3]Because k exceeds the number of videos, all three counts are returned. The two videos with 7 views remain separate entries.
Constraints
1 <= viewCounts.length <= 2 * 10^5.0 <= k <= 10^9.0 <= viewCounts[i] <= 10^9.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Top K Frequent ElementsAmazon · OA · PHONE SCREEN · ONSITE INTERVIEW
- Mutual-Friend RecommendationsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Tennis Court SchedulingAtlassian · PHONE SCREEN · ONSITE INTERVIEWAtlassian interview questions
- Retrieve Vectors by Cosine SimilarityHarvey · ONSITE INTERVIEWHarvey interview questions
- Meeting Rooms IISalesforce · ONSITE INTERVIEWSalesforce interview questions
- Top K Largest Values from a StreamThe D.E. Shaw Group · ONSITE INTERVIEWThe D.E. Shaw Group interview questions
- Generate Complete Inputs for a Supplied ClassifierGeneral Motors · ONSITE INTERVIEW
- Minimum Meeting Rooms Without a Sweep LineGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Top K Frequent ElementsCapgemini · ONSITE INTERVIEWCapgemini interview questions
- Kth Largest Element in an ArrayOneTrust · ONSITE INTERVIEW