Keep At Most Two Copies of Each Sorted Event ID In Place
Last updated: November 29, 2026
Quick Overview
Given a sorted array of event IDs, remove extra duplicates in place so that each ID appears at most twice, and return the new length.
Meta
November 29, 20260
0
0 solved
Given a sorted array of event IDs, remove extra duplicates in place so that each ID appears at most twice, and return the new length.
A Meta screen question about in-place compaction with a read and write pointer, framed around deduplicating replayed log events. The interviewer looks for the trick of comparing with the element two slots behind the write pointer.
What the Interviewer Expects
- Uses a read pointer and a write pointer over the same array
- Compares the current value with the value at write - 2
- Explains why sortedness makes that comparison sufficient
- Generalizes the code to at most k copies
- Gives O(n) time and O(1) extra space
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 keep at most k copies.
- What if the array is not sorted? What would you trade off?
- How would you do this on a linked list of events?
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: ids, sorted ascending. Modify it so the first L slots hold the kept IDs in order, each ID at most twice, and return L. Values past L do not matter. No extra array....
High-Level Design
Write pointer w marks where the next kept value goes. For each value x, keep it if w < 2 or x != ids[w-2]. Since the array is sorted, if x equals the value two kept slots back, then the two ...