FastPrepThree Partition Array
Problems/Three Partition Array

Three Partition Array

Microsoft logoMicrosoft● EasyPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: 🐼 The order of elements within each partition is not fixed, so multiple returned arrays may be correct. The example shows only one possible answer. FastPrep currently compares returned arrays with one stored result, so another valid partition may be marked incorrect. Please verify the partition conditions manually for now.

Given an array arr and a range [lowVal, highVal], partition the array into three parts:

  • All elements smaller than lowVal come first.
  • All elements in the range [lowVal, highVal] come next.
  • All elements greater than highVal come 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 = 0 and high = n - 1, where n is the length of arr.
  • While mid ≤ high, look at arr[mid]:
    • If it is smaller than lowVal, swap arr[low] and arr[mid], then add 1 to both low and mid.
    • If it is greater than highVal, swap arr[mid] and arr[high], then subtract 1 from high (do not move mid).
    • Otherwise, add 1 to mid.

Complete the function threePartitionArray in the editor.

threePartitionArray has the following parameters:

  • int[] arr: an array of integers
  • int lowVal: the lower bound of the range
  • int 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)
All Microsoft interview questionsSee Microsoft hiring insights
Saved
class Solution {
    public int[] threePartitionArray(int[] arr, int lowVal, int highVal) {
      // write your code here
    }

}
Checking account…