PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches

Quick Overview

This question evaluates proficiency in in-place array manipulation, constant-space algorithms, and linear-time partitioning techniques. Commonly asked in Coding & Algorithms interviews to assess algorithmic efficiency, pointer/index management, and reasoning about invariants, it focuses on practical application of algorithmic concepts rather than purely theoretical understanding.

  • medium
  • Microsoft
  • Coding & Algorithms
  • Software Engineer

Sort Three Categories In Place

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given an array `items` containing only the values `0`, `1`, and `2`, where each value represents a category. Rearrange the array in-place so that all `0`s come first, followed by all `1`s, then all `2`s. Requirements: - Modify the input array in-place. - Use `O(1)` extra space. - Aim for a single-pass `O(n)` solution. Example: ```text Input: items = [2, 0, 2, 1, 1, 0] Output: items = [0, 0, 1, 1, 2, 2] ```

Quick Answer: This question evaluates proficiency in in-place array manipulation, constant-space algorithms, and linear-time partitioning techniques. Commonly asked in Coding & Algorithms interviews to assess algorithmic efficiency, pointer/index management, and reasoning about invariants, it focuses on practical application of algorithmic concepts rather than purely theoretical understanding.

You are given an array `items` containing only the values `0`, `1`, and `2`, where each value represents a category. Rearrange the array in-place so that all `0`s come first, followed by all `1`s, then all `2`s. You must modify the input array in-place, use `O(1)` extra space, and aim for a single-pass `O(n)` solution. For testing, return the modified array after sorting.

Constraints

  • 0 <= len(items) <= 10^5
  • Each element in items is one of: 0, 1, 2
  • The solution must use O(1) extra space

Examples

Input: ([2, 0, 2, 1, 1, 0],)

Expected Output: [0, 0, 1, 1, 2, 2]

Explanation: After rearranging in-place, all 0s come first, then 1s, then 2s.

Input: ([0],)

Expected Output: [0]

Explanation: A single-element array is already sorted.

Input: ([],)

Expected Output: []

Explanation: An empty array remains empty.

Input: ([2, 2, 2, 1, 1, 0, 0],)

Expected Output: [0, 0, 1, 1, 2, 2, 2]

Explanation: The array contains all three categories in reverse-grouped order and must be rearranged in-place.

Input: ([1, 1, 1, 1],)

Expected Output: [1, 1, 1, 1]

Explanation: If all elements are the same category, the array does not change.

Input: ([2, 0, 1],)

Expected Output: [0, 1, 2]

Explanation: A small mixed example that requires swapping both ends correctly.

Hints

  1. Try maintaining three regions in the array: one for `0`s, one for unknown values, and one for `2`s.
  2. When you see a `2`, swap it toward the end, but do not immediately move past the new value swapped into the current position.
Last updated: May 15, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Implement K-Means and Detect Divisible Subarrays - Microsoft (medium)
  • Implement SFT Sample Packing - Microsoft (medium)
  • Implement SQL Table and DNA Ordering - Microsoft (medium)
  • Solve power jumps and graph tour - Microsoft (hard)
  • Implement concurrent structures and debug queue code - Microsoft (hard)