The Partition Array According to Given Pivot problem asks you to rearrange an array such that all elements less than pivot come first, then elements equal to pivot, then elements greater than pivot. The relative order within each group must be preserved. This coding problem is a stable three-way partition. The array, two pointers, and simulation interview pattern is demonstrated.
Apple, Microsoft, Amazon, Google, and Bloomberg ask this to test stable partitioning — where original relative order within groups must be maintained. This is distinct from Dutch National Flag (which doesn't require stability). The simple approach uses three separate lists.
Three-group collection. Maintain three lists: less, equal, greater. Traverse the array once: if element < pivot, append to less; if == pivot, append to equal; if > pivot, append to greater. Concatenate: less + equal + greater. This is O(n) time and O(n) space.
nums=[9,12,5,10,14,3,10], pivot=10.
Stable partition problems require an extra O(n) space solution (three lists) because in-place partitioning (Dutch National Flag) doesn't preserve relative order. Distinguish between: (1) Dutch National Flag (in-place, O(1) space, unstable), (2) stable 3-way partition (O(n) space, order-preserving). In interviews, clarify whether stability is required before choosing the approach.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Rearrange Array Elements by Sign | Medium | Solve | |
| Watering Plants II | Medium | Solve | |
| Apply Operations to an Array | Easy | Solve | |
| Find the Array Concatenation Value | Easy | Solve | |
| Adding Spaces to a String | Medium | Solve |