PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates understanding of in-place array manipulation, duplicate removal, and algorithmic analysis under strict extra-space constraints. It is commonly asked in Coding & Algorithms interviews to assess reasoning about time-space trade-offs and algorithmic complexity, emphasizing practical application and implementation-level skills rather than purely conceptual knowledge.

  • medium
  • Microsoft
  • Coding & Algorithms
  • Software Engineer

Remove duplicates in-place from unsorted array

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

## Problem You are given an array `nums` of **unsorted** integers. Write a method that **removes duplicates in-place** (i.e., without using any additional data structure such as a hash set/map/boolean array). The method should return the **number of unique elements** `k` and modify `nums` so that the first `k` positions contain the unique values. ## Requirements - Extra space: **O(1)** (ignore call stack). - The input array is **unsorted**. - You may **reorder** the array elements unless otherwise stated. ## Follow-up - Is an **O(n)** time solution possible under these constraints? If yes, state the needed assumptions; if not, explain why. ## Examples - Input: `[3, 1, 3, 2, 1]` → Output `k = 3`, first `k` elements are some permutation of `{1, 2, 3}`. - Input: `[]` → Output `k = 0`. ## Constraints - `0 ≤ n ≤ 10^5` - Integers fit in 32-bit signed range.

Quick Answer: This question evaluates understanding of in-place array manipulation, duplicate removal, and algorithmic analysis under strict extra-space constraints. It is commonly asked in Coding & Algorithms interviews to assess reasoning about time-space trade-offs and algorithmic complexity, emphasizing practical application and implementation-level skills rather than purely conceptual knowledge.

Sort the array in place, compact unique values to the front, and return [k, first_k_values] for deterministic grading.

Constraints

  • The array may be reordered.
  • The returned first_k_values are sorted unique values.
  • This uses no hash set or auxiliary array.

Examples

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

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

Explanation: Sorting enables O(1) extra-space compaction.

Input: ([],)

Expected Output: [0, []]

Explanation: Empty input has zero unique elements.

Input: ([5,5,5],)

Expected Output: [1, [5]]

Explanation: Only one unique value remains.

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

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

Explanation: Already unique values are returned sorted after in-place reordering.

Hints

  1. Sorting makes duplicates adjacent.
  2. Use a write pointer to compact unique values.
Last updated: Jun 27, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ 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

  • Return Top K Open Businesses - Microsoft (hard)
  • Implement Memory Allocation and In-Memory Records - Microsoft (medium)
  • Implement K-Means and Detect Divisible Subarrays - Microsoft (medium)
  • Sort Three Categories In Place - Microsoft (medium)
  • Retain Top K Elements - Microsoft (medium)