Top K Frequent Elements
Updated
Problem statement
Given an integer array nums and an integer k, return the k distinct values with the highest frequencies.
Order the answer by decreasing frequency. If two selected values have equal frequency, place the smaller value first.
Function
topKFrequent(nums: int[], k: int) → int[]Examples
Example 1
nums = [1,1,1,2,2,3]k = 2return = [1,2]1 occurs three times and 2 occurs twice.
Example 2
nums = [4,4,-1,-1,2]k = 2return = [-1,4]The selected values tie in frequency, so the smaller value comes first.
Constraints
1 <= nums.length <= 100000.-10^9 <= nums[i] <= 10^9.1 <= k <=the number of distinct values.
More Tennr Phone Screen questions
Similar questions at other companies
- Mutual-Friend RecommendationsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Filter, Sort, and Deduplicate Scheduled TasksRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Top K Frequent ElementsAmazon · OA · PHONE SCREEN
- Longest Consecutive SequenceJP Morgan Chase · PHONE SCREENJP Morgan Chase interview questions
- Top K Frequent Elements with a Recency TiebreakSwiggy · PHONE SCREEN
- Pins Violation Log QueriesPinterest · PHONE SCREENPinterest interview questions
- Group AnagramsWorkday · PHONE SCREEN
- Build a Task and Subtask HierarchyStripe · PHONE SCREENStripe interview questions
- Find All Points Within a RadiusGoogle · PHONE SCREENGoogle interview questions
- Resource Reconciler: Plan and Apply CreatesPerplexity AI · PHONE SCREENPerplexity AI interview questions