FastPrepRetain Top K Values
Problems/Retain Top K Values

Retain Top K Values

Microsoft logoMicrosoft● EasyFULLTIMENEW GRADPHONE SCREENONSITE INTERVIEW

Updated Oct 2026

QuestionFunctionExamplesConstraints

Problem statement

Given an integer array nums and an integer k, remove every value except the k largest elements. The relative order of the retained elements must be the same as in the original array.

If there are ties at the cutoff value, keep the earliest tied elements until exactly k elements have been retained.

Function

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

Examples

Example 1

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

The three largest elements are 5, 5, and 3. They are returned in their original order.

Example 2

nums = [4,4,4,2]k = 2return = [4,4]

When equal values cross the cutoff, keep the earliest occurrences needed to return exactly k elements.

Constraints

  • 1 <= nums.length <= 104
  • -104 <= nums[i] <= 104
  • 0 <= k <= nums.length
  • The return value must contain exactly k elements.
  • The relative order of retained elements must match their order in the original array.
  • When multiple elements share the cutoff value, keep the earliest-occurring tied elements until exactly k elements have been retained.

More Microsoft Phone Screen questions

  • Config Store with Priority OverridesPHONE SCREEN · Seen Nov 2026
  • Corporation Reporting LinesPHONE SCREEN · Seen Oct 2026
  • Random Forest Classifier from ScratchPHONE SCREEN · Seen Oct 2026
  • Least Recently Used CacheOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Greedy and Beam Sentence DecodingPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Top K Frequent ElementsPHONE SCREEN · Seen Sep 2026
  • 3SumPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Arithmetic Expression EvaluatorPHONE SCREEN · Seen Sep 2026
  • Employee Hierarchy Cycle and Depth AnalysisPHONE SCREEN · Seen Sep 2026
  • Most Visited String SectorsPHONE SCREEN · Seen Sep 2026

Similar questions at other companies

  • Majority ElementByteDance · PHONE SCREENByteDance interview questions
  • Group Transactions by HourAffirm · ONSITE INTERVIEWAffirm interview questions
  • Recommend Films from Viewers Who Watched Everything You DidBloomberg LP · PHONE SCREENBloomberg LP interview questions
  • Total Years of Work ExperienceLinkedIn · PHONE SCREENLinkedIn interview questions
  • Shared Courses for Every Pair of StudentsCrunchyroll · PHONE SCREENCrunchyroll interview questions
  • Employee Count Median, Mean and ModeClay · PHONE SCREEN
  • Merge Sorted ArrayDP World · ONSITE INTERVIEW
  • Squares of a Sorted ArrayInstacart · PHONE SCREENInstacart interview questions
  • Map and Accumulate Learning QueriesPalantir · ONSITE INTERVIEWPalantir interview questions
  • Assign CookiesAmazon · PHONE SCREEN

Interview reports that mention this question

  • Microsoft AI/ML Intern Interviews: 114 Experiences (Part 1 of 2)

Microsoft interview experiences

  • Microsoft OA Questions, October 2026: 4 Experiences
  • Microsoft Experienced SWE Phone Screen: 1 Experience
  • Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
  • Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
  • Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
All Microsoft interview questionsSee Microsoft hiring insights
Saved
class Solution {
    public int[] retainTopKValues(int[] nums, int k) {
        // write your code here
    }

}
Checking account…