Sort Server Health States Into Three Bands in One Pass
Last updated: November 29, 2026
Quick Overview
Given an array where each server's health is 0 (healthy), 1 (degraded) or 2 (down), reorder it in place so all 0s come first, then 1s, then 2s, using one pass and O(1) extra space.
0
0
0 solved
Given an array where each server's health is 0 (healthy), 1 (degraded) or 2 (down), reorder it in place so all 0s come first, then 1s, then 2s, using one pass and O(1) extra space.
A Google phone screen question framed around a Borg-style dashboard that groups machines by health. Counting sort is the easy two-pass answer, so the interviewer pushes for Dijkstra's Dutch national flag partition and checks the loop invariants carefully.
What the Interviewer Expects
- Mentions two-pass counting sort as a baseline
- Implements three-pointer Dutch national flag partition
- States and maintains the invariant for each region
- Avoids advancing the middle pointer after swapping with the high side
- Tests with all-equal and already-sorted inputs
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Generalize to k states; what is the best approach?
- Make the partition stable relative to the original order; what does it cost?
- How would you partition around a pivot value for quickselect using the same idea?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: list
hof values in {0, 1, 2}. - Output: the same list rearranged in place to non-decreasing order.
- Constraints: single pass, O(1) extra space....
High-Level Design
Keep three pointers: lo, mid, hi. Invariant: h[:lo] are all 0, h[lo:mid] are all 1, h[mid:hi+1] is unprocessed, and h[hi+1:] are all 2. Look at h[mid]:
- 0: swap with
h[lo], advance...