By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
(A Complete FAANG-Level Interview Guide)
"This problem appears in 1 out of every 3 onsite interviews—master it, and you prove you can solve in-place array problems with O(1) extra space, a skill that separates L4 from L5 engineers at FAANG."
Before tackling this problem, ensure you understand: 1. Array indexing & in-place modification – How to use indices as markers without extra space. 2. Cyclic sort – A pattern for rearranging elements to their correct positions in O(n) time. 3. Hash sets vs. in-place marking – Trade-offs between space and time complexity.
nums[i]
nums[i]-1
n(n+1)/2
(Repeatable mental checklist for every "disappeared numbers" problem.)
What is the range of numbers? (If 1..n, cyclic sort or negation works well.)
Choose the Right Technique
Numbers in 1..n? → Cyclic sort is optimal.
Implement the Chosen Approach
x
nums[abs(x) - 1]
For cyclic sort:
nums[i] - 1
Edge Cases
Duplicates? → Ensure marking doesn’t overwrite (use abs()).
abs()
Optimize & Verify
[4,3,2,7,8,2,3,1]
[5,6]
Problem: Given an array nums of n integers where nums[i] is in [1, n], return an array of all numbers in [1, n] that do not appear in nums.
nums
n
[1, n]
Input: nums = [4,3,2,7,8,2,3,1] Output: [5,6]
nums = [4,3,2,7,8,2,3,1]
Step-by-Step Solution: 1. Clarify Constraints: - Numbers are in 1..n (n = 8). - O(1) space required (no extra arrays). - Duplicates exist (2 and 3 appear twice).
1..n
2
3
In-place negation (O(1) space, handles duplicates).
Algorithm:
The indices of positive numbers at the end are missing.
Code (Python):
def findDisappearedNumbers(nums): for num in nums: idx = abs(num) - 1 if nums[idx] > 0: # Avoid double negation nums[idx] = -1 result = [] for i in range(len(nums)): if nums[i] > 0: result.append(i + 1) return result
Space: O(1) (in-place).
Why This Works:
Problem: Same as above, but without duplicates and O(1) space required.
Input: nums = [4,3,2,7,8,2,3,1] → Wait, duplicates exist! Modified Input: nums = [4,3,2,7,8,1,6,5] (no duplicates) Output: [9] (if n=9)
nums = [4,3,2,7,8,1,6,5]
[9]
n=9
Step-by-Step Solution: 1. Clarify Constraints: - No duplicates. - Numbers in 1..n. - O(1) space.
Cyclic sort (optimal for 1..n without duplicates).
After sorting, iterate to find mismatches.
def findDisappearedNumbers(nums): i = 0 while i < len(nums): correct_pos = nums[i] - 1 if nums[i] != nums[correct_pos]: nums[i], nums[correct_pos] = nums[correct_pos], nums[i] else: i += 1 result = [] for i in range(len(nums)): if nums[i] != i + 1: result.append(i + 1) return result
Space: O(1).
Problem: Given an array nums of n integers where nums[i] is in [1, n], return: 1. All numbers that do not appear in nums. 2. The number that appears twice.
Input: nums = [4,3,2,7,8,2,3,1] Output: [5,6] (missing), 2 (duplicate)
Step-by-Step Solution: 1. Clarify Constraints: - Duplicates exist. - O(1) space required.
In-place negation (marks seen numbers and detects duplicates).
def findErrorNums(nums): duplicate = -1 for num in nums: idx = abs(num) - 1 if nums[idx] < 0: duplicate = abs(num) else: nums[idx] = -1 missing = -1 for i in range(len(nums)): if nums[i] > 0: missing = i + 1 break return [duplicate, missing]
i+1
i
nums = []
[]
nums = [1,2,3]
"Alright, let’s lock this in. When you see ‘Find All Numbers Disappeared in an Array,’ here’s your playbook:
nums[abs(x)-1]
Test edge cases: empty array, all numbers present, duplicates. And remember—always ask clarifying questions before coding. You’ve got this!
Now go crush that interview! ?
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.