FastPrepKeep at Most K Occurrences in a Sorted Array
Problems/Keep at Most K Occurrences in a Sorted Array

Keep at Most K Occurrences in a Sorted Array

Oracle logoOracle● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an Oracle onsite interview. The report asks you to change a sorted integer array in place so each value appears at most k times, in O(n) time and O(1) space. It does not give what to return, what to do when k is 0, examples, or the input limits. Here you return the length of the cleaned-up front part of the array, and the in-place idea stays. Our version is about 94% the same as the reported question.

You are given an integer array nums sorted in nondecreasing order and a nonnegative integer k. Compact nums in place so that every distinct value appears at most k times while preserving sorted order.

Return an array containing exactly the compacted prefix. The compaction must use O(1) auxiliary space; the returned array used to expose that prefix to the judge is not counted as auxiliary workspace. If k is 0, return an empty array.

Function

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

Examples

Example 1

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

The third 1 is removed; every other occurrence fits within the limit of two.

Example 2

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

Only the first occurrence of each distinct value remains.

Example 3

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

A zero occurrence limit removes every value.

Constraints

  • 0 <= nums.length <= 100000
  • -1000000000 <= nums[i] <= 1000000000
  • 0 <= k <= nums.length when nums is non-empty.
  • k = 0 when nums is empty.
  • nums is sorted in nondecreasing order.

More Oracle Onsite Interview questions

  • Course ScheduleONSITE INTERVIEW · Seen Nov 2026
  • Shortest Subarray with Sum at Least KONSITE INTERVIEW · Seen Nov 2026
  • Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
  • Minimum Meeting RoomsONSITE INTERVIEW · Seen Nov 2026
  • Shortest Window with Required CountsONSITE INTERVIEW · Seen Nov 2026
  • Maximum Subarray Sum with One DeletionONSITE INTERVIEW · Seen Nov 2026
  • Shortest Deletion for Distinct CharactersONSITE INTERVIEW · Seen Nov 2026
  • Count Dropped Rate-Limited RequestsONSITE INTERVIEW · Seen Nov 2026
  • Design Authentication ManagerONSITE INTERVIEW · Seen Oct 2026
  • Maximum Bouquet ValueONSITE INTERVIEW · Seen Oct 2026

Similar questions at other companies

  • Valid Triangle NumberBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
  • Count Winning Dice Face PairsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
  • Merge Sorted Squares of Two ArraysWhatNot · PHONE SCREEN · ONSITE INTERVIEWWhatNot interview questions
  • Run-Length Encoded Vector Dot ProductMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
  • Search a Row- and Column-Sorted MatrixByteDance · ONSITE INTERVIEWByteDance interview questions
  • 3Sum SmallerSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Sort Ones, Twos, and Threes In PlaceAgoda · PHONE SCREEN · ONSITE INTERVIEWAgoda interview questions
  • Minimum Station Tracks for a Train ScheduleBNSF Railway · ONSITE INTERVIEWBNSF Railway interview questions
  • Candy Crush Grid Matching and GravityRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
  • Longest Binary Window with at Most K OnesLinkedIn · ONSITE INTERVIEWLinkedIn interview questions

Oracle interview experiences

  • Oracle SWE Onsite Interview Experience: 1 Experience
All Oracle interview questionsSee Oracle hiring insights
Saved
class Solution {
    public int[] keepAtMostK(int[] nums, int k) {
      // write your code here
    }

}
Checking account…