Practice
findDuplicate([2, 1, 4, 3, 2])?Solution
Step 1: Trace initial array and i=0
nums = [2,1,4,3,2], i=0, correct_index=1. nums[0]=2 != nums[1]=1, swap -> nums=[1,2,4,3,2]Step 2: i=0 again, correct_index=0, nums[0]=1 == nums[0]=1, i=1
i=1, correct_index=1, nums[1]=2 == nums[1]=2, i=2Step 3: i=2, correct_index=3, nums[2]=4 != nums[3]=3, swap -> nums=[1,2,3,4,2]
i=2 againStep 4: i=2, correct_index=2, nums[2]=3 == nums[2]=3, i=3
i=3, correct_index=3, nums[3]=4 == nums[3]=4, i=4Step 5: i=4, correct_index=1, nums[4]=2 == nums[1]=2, i != correct_index -> duplicate found: 2
Final Answer:
Option A -> Option AQuick Check:
Duplicate 2 correctly identified [OK]
- Returning wrong duplicate due to off-by-one index
- Incrementing i too early causing missed duplicates
- Confusing value and index during swaps
findKthPositive([2,3,7], 2)?Solution
Step 1: Trace binary search iterations
Initial low=0, high=2 mid=1, arr[mid]=3, missing=3-(1+1)=1 < k=2 -> low=mid+1=2 mid=2, arr[mid]=7, missing=7-(2+1)=4 >= k=2 -> high=mid-1=1Step 2: Compute final return value
Return k + high + 1 = 2 + 1 + 1 = 4 Loop ends as low=2, high=1.Final Answer:
Option D -> Option DQuick Check:
Binary search converges with high=1, return 4 [OK]
- Off-by-one in missing calculation
- Returning low instead of k+high+1
Solution
Step 1: Analyze the while loop condition and break
The while loop runs while nums[i] is in range, but the break triggers if nums[nums[i]-1] == nums[i]. This is correct to avoid infinite swaps.Step 2: Identify the subtle bug
However, the break inside the while loop causes premature exit before all necessary swaps, leading to incorrect placement and wrong results.Final Answer:
Option B -> Option BQuick Check:
Removing the break and using the while condition properly fixes the bug [OK]
- Misplaced break causing incomplete sorting
- Incorrect final return value
- Wrong loop boundaries
Solution
Step 1: Understand the problem extension
Multiple duplicates and missing numbers require identifying all mismatches after cyclic sort.Step 2: Modify cyclic sort approach
Perform cyclic sort once to place numbers as close as possible to correct indices, then scan to collect all mismatches (duplicates and missing).Step 3: Evaluate other options
Using hash sets adds extra space; repeated cyclic sorts are inefficient; sorting is O(n log n).Final Answer:
Option B -> Option BQuick Check:
Single cyclic sort + scan finds all mismatches efficiently [OK]
- Trying repeated cyclic sorts wastes time
- Adding hash sets breaks O(1) space constraint
- Using sorting loses linear time advantage
Solution
Step 1: Understand reuse impact
Infinite reuse means cyclic sort assumptions break because numbers can appear multiple times and be reused infinitely.Step 2: Identify correct approach
A frequency map tracks counts of each number, allowing detection of missing positives even with infinite reuse.Step 3: Why other options fail
Adding visited set won't prevent infinite swaps due to reuse. Brute force is inefficient. Priority queue adds overhead without correctness guarantee.Final Answer:
Option A -> Option AQuick Check:
Frequency map handles reuse correctly and efficiently [OK]
- Trying to patch cyclic sort with visited sets
- Assuming brute force is acceptable for large k
- Using priority queues unnecessarily
