FastPrepKeep At Most Two Copies in a Sorted Array
Problems/Keep At Most Two Copies in a Sorted Array

Keep At Most Two Copies in a Sorted Array

Adobe logoAdobe● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: You trim a sorted array so that no value appears more than twice. An Adobe report for a CS-1 role, by an engineer in India with 4.5 years of experience (interviewed in December 2023, rejected), names this task with a link as the first of two medium questions in the opening DSA round. It adds that the author did not manage to code the faster version. Beyond that, it gives no limits or examples.

The rules are those of the named task. The input is sorted. Each value keeps at most two copies. The kept values stay in order. We chose these rules. The function returns the kept values as an array instead of a count. The editorial still works inside the array. There are up to 2000 values, from -10^4 to 10^4. We wrote new examples.

Our version is about 90% the same as the reported question.

You are given an integer array nums sorted from smallest to largest. Remove extra copies so that every value appears at most twice, keeping the remaining values in their original order.

Return the kept values as an array. Try to do the work inside nums itself with only a few extra variables, as you would if the array could not be copied; the judge checks only the returned values.

Function

keepAtMostTwo(nums: int[]) → int[]

Examples

Example 1

nums = [-4,-4,-4,0,3,3,3,3,8]return = [-4,-4,0,3,3,8]

-4 appears three times and 3 appears four times; each keeps two copies. 0 and 8 appear once and stay. Result: [-4,-4,0,3,3,8].

Example 2

nums = [2,5,9]return = [2,5,9]

No value appears more than twice, so nothing is removed.

Example 3

nums = [6,6,6,6,6]return = [6,6]

Only two copies of 6 are kept.

Constraints

  • 1 <= nums.length <= 2000
  • -10^4 <= nums[i] <= 10^4
  • nums is sorted from smallest to largest

More Adobe Phone Screen questions

  • Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
  • All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
  • Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
  • Binary Tree Level-Order Traversal by LevelsPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
  • Count Occurrences in a Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
  • Count Pairs with Absolute Difference KPHONE SCREEN · Seen Jul 2026
  • Count Subsets with Sum KPHONE SCREEN · Seen Jul 2026
  • Numbers with Exactly Three DivisorsPHONE SCREEN · Seen Jul 2026
  • Sum of Even Nodes on a Path in a Numbered Binary TreePHONE SCREEN · Seen Jul 2026
  • Largest Value in Every Sliding WindowPHONE SCREEN · Seen Jul 2026

Similar questions at other companies

  • Subarray Product Less Than KApple · PHONE SCREENApple interview questions
  • 3SumAmazon · PHONE SCREEN
  • Subarray Product Less Than KAngel One · PHONE SCREEN
  • Subarray Product Less Than KThe D.E. Shaw Group · PHONE SCREENThe D.E. Shaw Group interview questions
  • Trapping Rain WaterGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
  • Sort Ones, Twos, and Threes In PlaceAgoda · PHONE SCREEN · ONSITE INTERVIEWAgoda interview questions
  • Candy Crush Grid Matching and GravityRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
  • Find All Pythagorean TripletsDell Technologies · PHONE SCREENDell Technologies interview questions
  • Meeting Rooms IIUber · PHONE SCREENUber interview questions
  • Sort a Quadratic-Transformed ArrayLinkedIn · PHONE SCREENLinkedIn interview questions

Interview reports that mention this question

  • Adobe CS1 Interviews: 14 Experiences (Part 4 of 6)
  • Adobe Experienced Hire Interviews: 55 Experiences (Part 1 of 6)

Adobe interview experiences

  • Adobe Web, QA, DevOps, ML Roles: 13 Experiences (Part 3 of 3)
  • Adobe SWE Interviews, Level Unknown: 25 Experiences (Part 2 of 3)
  • Adobe Interviews, Level Unknown: 38 Experiences (Part 1 of 3)
  • Adobe New Grad MTS Interview Questions: 7 Experiences
All Adobe interview questionsSee Adobe hiring insights
Saved
class Solution {
    public int[] keepAtMostTwo(int[] nums) {
        // Write your code here.
    }
}
Checking account…