FastPrepTop K Frequent Elements
Problems/Top K Frequent Elements

Top K Frequent Elements

Tennr logoTennr● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A firsthand Tennr Implementation Engineer report describes Top K Frequent Elements in the second-round DSA interview. Our version follows the standard task and adds a fixed order for ties so answers can be checked.

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

  • Wildcard Multi-Delimiter ValidationPHONE SCREEN · Seen Aug 2025
  • Find First and Last Position in Sorted ArrayPHONE SCREEN · Seen Feb 2025

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
See Tennr hiring insights
Saved
class Solution {
    public int[] topKFrequent(int[] nums, int k) {
        // Write your code here.
    }
}
Checking account…