FastPrepTop K Frequent Integers
Problems/Top K Frequent Integers

Top K Frequent Integers

Meta logoMeta● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Meta onsite interview. The report asks for the top k frequent integers and has a follow-up about building a heap in linear time. We chose how to break ties in the output and the limits. Our version is about 90% the same as the reported question.

Given an integer array nums and an integer k, return the k distinct values with the highest frequencies.

For this exercise, assume the result is ordered by decreasing frequency. When values have equal frequency, put the smaller integer first. Apply that tie rule when a tie crosses the kth position as well.

Follow-up

After counting frequencies, explain why building a heap from all distinct values can take linear time in the number of distinct values, even though inserting each one separately may take longer.

Function

topKFrequent(nums: int[], k: int) → int[]

Examples

Example 1

nums = [1,1,1,2,2,3]k = 2return = [1,2]

Value 1 appears three times and value 2 appears twice, so return [1,2].

Example 2

nums = [4,4,-2,-2,7,7,3]k = 2return = [-2,4]

Values -2,4,7 each appear twice. The smaller two win the cutoff tie, in increasing numeric order.

Example 3

nums = [9,-3,2]k = 3return = [-3,2,9]

All frequencies are one, so all distinct values appear in increasing numeric order.

Example 4

nums = [5,5,5]k = 1return = [5]

There is only one distinct value, regardless of its number of occurrences.

Constraints

  • For this exercise, assume 1 <= nums.length <= 5000.
  • -10^9 <= nums[i] <= 10^9.
  • 1 <= k <= the number of distinct values in nums.

More Meta Onsite Interview questions

  • Mutual-Friend RecommendationsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Deterministic K-Means AssignmentsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Linear Layer Forward and Backward PassesONSITE INTERVIEW · Seen Sep 2026
  • Reservoir Sampling with Recorded DrawsONSITE INTERVIEW · Seen Sep 2026
  • Stochastic Gradient Descent with MomentumONSITE INTERVIEW · Seen Sep 2026
  • Top K Largest Values with a HeapONSITE INTERVIEW · Seen Sep 2026
  • Multi-Head Attention Forward PassPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Partition a Card Deck into FifteensONSITE INTERVIEW · Seen Sep 2026
  • Insert into a Sorted Circular ListONSITE INTERVIEW · Seen Aug 2026
  • Maximum Stock Profit with Transaction LimitsONSITE INTERVIEW · Seen Aug 2026

Similar questions at other companies

  • Top K Frequent ElementsCapgemini · ONSITE INTERVIEWCapgemini interview questions
  • Top K Frequent Elements with Larger-Value Tie BreakOracle · ONSITE INTERVIEWOracle interview questions
  • Top K Frequent ElementsInfosys · ONSITE INTERVIEWInfosys interview questions
  • Stable Top K Frequent WordsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
  • Top K Frequent ElementsBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
  • Top K Frequent ElementsAmazon · OA · PHONE SCREEN
  • Rearrange Songs by AuthorZomato / Eternal · OAZomato / Eternal interview questions
  • Top K Frequent ElementsApple · PHONE SCREENApple interview questions
  • Top K Frequent ElementsC3.ai · PHONE SCREENC3.ai interview questions
  • Smallest Range Covering Elements from K ListsSnap Inc. · PHONE SCREENSnap Inc. interview questions

Meta interview experiences

  • Meta PhD SWE Intern AI Coding Phone Screen: 1 Experience
  • Meta Business Support Engineer Phone Screen: 1 Experience
  • Meta Software Engineer Onsite Questions: 3 Experiences
  • Meta AI Native Engineer OA and Onsite: 1 Experience
  • Meta AI-Native Coding Interview: 3 Experiences
  • Meta Production Engineer Intern Questions: 6 Experiences
All Meta interview questionsSee Meta hiring insights
Saved
class Solution {
    public int[] topKFrequent(int[] nums, int k) {
        // Write your code here.
    }
}
Checking account…