Sort Colors
Updated
Problem statement
You are given an integer array nums containing only 0, 1, and 2. Rearrange the array in place so that equal values are adjacent and the values appear in the order 0, 1, then 2.
Do not call a library sorting routine. Return the same array after rearranging it so the result can be evaluated.
Function
sortColors(nums: int[]) → int[]Examples
Example 1
nums = [2,0,2,1,1,0]return = [0,0,1,1,2,2]The two zeros come first, followed by the two ones and the two twos.
Example 2
nums = [2,0,1]return = [0,1,2]Each color occurs once.
Constraints
1 <= nums.length <= 100000nums[i]is0,1, or2.- The rearrangement must use constant auxiliary space.
More Navan interview questions
- Roman to IntegerPHONE SCREEN · Seen Oct 2024
- Basic CalculatorPHONE SCREEN · Seen Jun 2024
- LRU CachePHONE SCREEN · Seen Jun 2024
- House RobberPHONE SCREEN · Seen May 2024
- House Robber IIPHONE SCREEN · Seen May 2024
- Snake-Case Iterator to Camel CasePHONE SCREEN · Seen Feb 2024
- Lexicographically Largest Distinct-Letter SubsequencePHONE SCREEN · Seen May 2023
- Time Based Key-Value StoreONSITE INTERVIEW · Seen Dec 2025
- Type-Ahead Prefix MatchesONSITE INTERVIEW · Seen Dec 2024
- Minimum Steps on a Circular Character DialONSITE INTERVIEW · Seen Jan 2024
Similar questions at other companies
- Merge Sorted Squares of Two ArraysWhatNot · PHONE SCREEN · ONSITE INTERVIEWWhatNot interview questions
- 3SumAmazon · PHONE SCREEN
- Sort Ones, Twos, and Threes In PlaceAgoda · PHONE SCREEN · ONSITE INTERVIEWAgoda 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 ColorsVisa · PHONE SCREENVisa interview questions
- Count Triplets Within a Value RangeIBM · PHONE SCREENIBM interview questions
- 3SumMicrosoft · PHONE SCREEN · ONSITE INTERVIEWMicrosoft interview questions
- Meeting Rooms IIWalmart · PHONE SCREENWalmart interview questions