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 write clean, in-place algorithms while handling edge cases like a pro."
Before diving in, ensure you understand: 1. Two-pointer technique (fast & slow pointers) 2. In-place array modification (avoiding extra space) 3. Time/space complexity trade-offs (O(1) space vs. O(n) space)
slow=0, fast=1; if nums[fast] != nums[slow], copy
if len(nums) <= 1: return len(nums)
if nums[fast] == nums[fast-1]: continue
(Repeatable mental checklist for every "Remove Duplicates" problem)
slow=0
fast=1
fast
nums[fast]
nums[slow]
slow
slow + 1
[1,1,2]
[0,0,1,1,1,2,2,3,3,4]
Problem: Remove duplicates from a sorted array in-place. Return the new length.
Solution (Python):
def removeDuplicates(nums): if not nums: return 0 slow = 0 for fast in range(1, len(nums)): if nums[fast] != nums[slow]: slow += 1 nums[slow] = nums[fast] return slow + 1
Time Complexity: O(n) (single pass) Space Complexity: O(1) (in-place)
Why this works: - slow tracks the last unique element. - fast scans for new unique elements. - Copying nums[fast] to nums[slow+1] ensures uniqueness.
nums[slow+1]
Problem: Remove duplicates so that each element appears at most twice.
def removeDuplicates(nums): if len(nums) <= 2: return len(nums) slow = 2 for fast in range(2, len(nums)): if nums[fast] != nums[slow - 2]: nums[slow] = nums[fast] slow += 1 return slow
Time Complexity: O(n) Space Complexity: O(1)
Why this works: - slow starts at 2 (allowing two duplicates). - Compare nums[fast] with nums[slow-2] (not slow-1). - Ensures no more than two duplicates.
2
nums[slow-2]
slow-1
Problem: Remove duplicates so that each element appears at most K times.
def removeDuplicates(nums, k): if len(nums) <= k: return len(nums) slow = k for fast in range(k, len(nums)): if nums[fast] != nums[slow - k]: nums[slow] = nums[fast] slow += 1 return slow
Why this works: - slow starts at k (allowing k duplicates). - Compare nums[fast] with nums[slow - k] (not slow - 1). - Generalizes to any k.
k
nums[slow - k]
slow - 1
for i in range(len(nums))
nums
while
[]
[1]
if len(nums) <= 1
len(nums) <= k
"Alright, let’s lock this in. For Remove Duplicates from Sorted Array: 1. Edge cases first—empty array? Single element? Return early. 2. Two pointers—slow tracks unique elements, fast scans. 3. Compare nums[fast] with nums[slow]—if different, copy and increment slow. 4. Return slow + 1—that’s the new length. 5. For K duplicates, compare with nums[slow - k] instead of nums[slow - 1].
nums[slow - 1]
Practice these variations: - Basic (LeetCode 26) - At most two duplicates (LeetCode 80) - General K duplicates (follow-up)
You’ve got this—now go crush that interview! ?"
Next steps: Implement all three examples in your preferred language. Time yourself—aim for <5 minutes per problem. ?
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.