Partition Moderation Labels Into Safe Review and Remove in One Pass

Last updated: November 29, 2026

Quick Overview

Given an array of content moderation labels where 0 is safe, 1 is needs review and 2 is remove, reorder it in place so all 0s come first, then 1s, then 2s, using one pass and constant extra space.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Medium

0

0

0 solved


Given an array of content moderation labels where 0 is safe, 1 is needs review and 2 is remove, reorder it in place so all 0s come first, then 1s, then 2s, using one pass and constant extra space.

The Dutch national flag problem framed around Meta's integrity queues. The interviewer wants the three pointer invariant stated clearly and wants to see that you do not advance the middle pointer after swapping with the high side.

What the Interviewer Expects
  • States the invariant for the four regions (0s, 1s, unknown, 2s)
  • Uses low, mid and high pointers in a single pass
  • Does not advance mid after a swap with high, since the swapped-in value is unexamined
  • Mentions counting sort as a two-pass alternative
  • Gives O(n) time and O(1) space
Key Topics to Cover
two pointers
dutch national flag
in-place partition
invariants
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Extend to k label types. What is the best you can do in place?
  • Make the partition stable with respect to original order. What does it cost?
  • How would you do this for labels stored in a linked list?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: labels, a list containing only 0, 1 and 2. Modify it in place into non-decreasing order. Return it for testing. Single pass, O(1) extra memory....

High-Level Design

Maintain: labels[0:low] are 0s, labels[low:mid] are 1s, labels[mid:high+1] are unknown, and labels[high+1:] are 2s. Look at labels[mid]. A 0 swaps into the low region, and both low and mid a...


Submit Your Answer
Markdown supported