Three Partition Array
Updated
Problem statement
Given an array arr and a range [lowVal, highVal], partition the array into three parts:
- All elements smaller than
lowValcome first. - All elements in the range
[lowVal, highVal]come next. - All elements greater than
highValcome last.
Many arrays satisfy these rules, so the answer is fixed by the standard one-pass, in-place method with three pointers (the "Dutch national flag" method, as in the Sort Colors problem). Return the array exactly as this method leaves it:
- Start with
low = 0,mid = 0andhigh = n - 1, wherenis the length ofarr. - While
mid ≤ high, look atarr[mid]:- If it is smaller than
lowVal, swaparr[low]andarr[mid], then add 1 to bothlowandmid. - If it is greater than
highVal, swaparr[mid]andarr[high], then subtract 1 fromhigh(do not movemid). - Otherwise, add 1 to
mid.
- If it is smaller than
Complete the function threePartitionArray in the editor.
threePartitionArray has the following parameters:
int[] arr: an array of integersint lowVal: the lower bound of the rangeint highVal: the upper bound of the range
The function returns an int[]: the partitioned array.
Function
threePartitionArray(arr: int[], lowVal: int, highVal: int) → int[]Examples
Example 1
arr = [1, 14, 5, 20, 4, 2, 54, 20, 87, 98, 3, 1, 32]lowVal = 14highVal = 20return = [1, 5, 4, 2, 1, 3, 14, 20, 20, 98, 87, 32, 54]Elements smaller than 14 are 1, 5, 4, 2, 3, 1; elements from 14 to 20 are 14, 20, 20; elements greater than 20 are 54, 87, 98, 32. The three-pointer method swaps each small element to the front as it meets it and swaps each large element with the element at high, which leaves [1, 5, 4, 2, 1, 3, 14, 20, 20, 98, 87, 32, 54]. For example, the first 54 it meets is swapped with the last element 32, and 32 is then checked again.
Constraints
1 ≤ arr.length ≤ 105-109 ≤ arr[i] ≤ 109-109 ≤ lowVal ≤ highVal ≤ 109
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
- Squares of a Sorted ArrayInstacart · PHONE SCREENInstacart interview questions
- Assign CookiesAmazon · PHONE SCREEN
- Merge Two Sorted ArraysCapgemini · PHONE SCREENCapgemini interview questions
- Squares of a Sorted ArrayStryker · PHONE SCREEN
- Squares of a Sorted ArrayGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
- Merge Into the Zero-Padded ArrayAdobe · PHONE SCREENAdobe interview questions
- Sorted Squares in Descending OrderWhatNot · PHONE SCREENWhatNot interview questions
- Assign CookiesExpedia · PHONE SCREEN · ONSITE INTERVIEWExpedia interview questions
- Merge Two Sorted ArraysZillow · PHONE SCREENZillow interview questions
- Merge Sorted Arrays with DuplicatesMygate · PHONE SCREENMygate interview questions
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)
- Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)