Remove duplicates in-place from unsorted array
Company: Microsoft
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
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.
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
- Sorting makes duplicates adjacent.
- Use a write pointer to compact unique values.