Find All Duplicates in an Array
Given an array of integers, where $1 \leq a[i] \leq n$, some elements appear twice and others appear once. Find all the elements that appear twice. Solve without extra space and in $O(n)$ runtime.
Why Interviewers Ask This
Meta interviewers ask this to evaluate your mastery of in-place array manipulation and index-based hashing. They specifically test if you can optimize space complexity to O(1) while maintaining linear time, a common requirement for high-scale systems where memory is constrained. This problem reveals whether you understand how to repurpose existing data structures creatively rather than relying on standard library helpers.
How to Answer This Question
Key Points to Cover
- Explicitly mention the constraint mapping values to indices (1 to n)
- Demonstrate the in-place negation technique clearly
- Explain why O(1) space is achieved without auxiliary data structures
- Confirm the algorithm handles duplicates correctly without false positives
- Discuss potential side effects of modifying the input array
Sample Answer
Common Mistakes to Avoid
- Using a HashSet or frequency map, which violates the O(1) space constraint
- Sorting the array first, which increases time complexity to O(n log n)
- Failing to handle the absolute value when accessing indices after negation
- Not clarifying if modifying the original input array is permitted
Practice This Question with AI
Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.