Sort Ones, Twos, and Threes In Place
Updated
Problem statement
You are given an integer array nums containing only 1, 2, and 3. Rearrange it in place so all ones come first, followed by all twos and then all threes.
Use constant auxiliary space and return the same array after rearranging it so the result can be evaluated.
Function
sortOneTwoThree(nums: int[]) → int[]Examples
Example 1
nums = [3,1,3,2,2,1]return = [1,1,2,2,3,3]The two ones come first, followed by the two twos and the two threes.
Example 2
nums = [3,1,2]return = [1,2,3]Each permitted value occurs once.
Constraints
1 <= nums.length <= 100000.- Every value is
1,2, or3. - The rearrangement must use constant auxiliary space.
More Agoda interview questions
- Molecular Weight of a Grouped FormulaONSITE INTERVIEW · Seen Oct 2026
- Minimum Daily Printing LimitONSITE INTERVIEW · Seen Aug 2026
- Climb Stairs with One, Two, or Three StepsONSITE INTERVIEW · Seen Aug 2026
- Rank Transform an ArrayONSITE INTERVIEW · Seen Aug 2026
- Minimum Absolute Difference PairsONSITE INTERVIEW · Seen Jun 2026
- Feasibility of Printing Within Given DaysONSITE INTERVIEW · Seen May 2026
- 3SumONSITE INTERVIEW · Seen May 2026
- Can Place FlowersPHONE SCREEN · Seen Nov 2026
- Reverse Words in a StringPHONE SCREEN · Seen Nov 2026
- Disk Space Window AnalyzerPHONE SCREEN · Seen Sep 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
- 3SumAmazon · PHONE SCREEN
- 3Sum SmallerSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Minimum Station Tracks for a Train ScheduleBNSF Railway · ONSITE INTERVIEWBNSF Railway 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
- Sort ColorsCapital One · ONSITE INTERVIEWCapital One interview questions