Practice
1. Given the following code snippet, what is the output when the input array is
[2, 3, 1, 5, 3]?
def find_duplicate_missing(nums):
i = 0
n = len(nums)
while i < n:
correct_index = nums[i] - 1
if nums[i] != nums[correct_index]:
nums[i], nums[correct_index] = nums[correct_index], nums[i]
else:
i += 1
for i in range(n):
if nums[i] != i + 1:
return [nums[i], i + 1]
return [-1, -1]
print(find_duplicate_missing([2, 3, 1, 5, 3]))
easy
Solution
Step 1: Trace swaps in the while loop
Initial array: [2,3,1,5,3] - i=0: nums[0]=2, correct_index=1, swap nums[0] and nums[1] -> [3,2,1,5,3] - i=0: nums[0]=3, correct_index=2, swap nums[0] and nums[2] -> [1,2,3,5,3] - i=0: nums[0]=1, correct_index=0, nums[0]==nums[correct_index], i=1 - i=1: nums[1]=2, correct_index=1, nums[1]==nums[correct_index], i=2 - i=2: nums[2]=3, correct_index=2, nums[2]==nums[correct_index], i=3 - i=3: nums[3]=5, correct_index=4, swap nums[3] and nums[4] -> [1,2,3,3,5] - i=3: nums[3]=3, correct_index=2, nums[3]==nums[correct_index], i=4 - i=4: nums[4]=5, correct_index=4, nums[4]==nums[correct_index], i=5 (exit loop)Step 2: Identify mismatch in second loop
Check indices: - i=0: nums[0]=1 correct - i=1: nums[1]=2 correct - i=2: nums[2]=3 correct - i=3: nums[3]=3 incorrect, expected 4 -> duplicate=3, missing=4 Return [3,4]Final Answer:
Option C -> Option CQuick Check:
Output matches expected duplicate and missing [OK]
Hint: Trace swaps carefully to find final array mismatch [OK]
Common Mistakes:
- Confusing duplicate and missing values
- Off-by-one errors in indexing
- Stopping swaps too early
2. You are given an array containing n distinct numbers taken from the range 0 to n. Exactly one number from this range is missing in the array. Which algorithmic approach guarantees finding the missing number in O(n) time and O(1) space without using extra data structures?
easy
Solution
Step 1: Understand problem constraints
The array contains numbers from 0 to n with one missing; all are distinct.Step 2: Identify approach with O(n) time and O(1) space
Cyclic sort places each number at its index in one pass, then a single scan finds the missing number efficiently.Final Answer:
Option D -> Option DQuick Check:
Cyclic sort rearranges in-place with linear time and constant space [OK]
Hint: Cyclic sort matches numbers to indices in-place [OK]
Common Mistakes:
- Assuming sorting is always O(n log n)
- Thinking hash sets use O(1) space
- Confusing DP with presence checking
3. Consider the following buggy code for finding the duplicate number. Which line contains the subtle bug that can cause an infinite loop or incorrect result?
medium
Solution
Step 1: Compare with correct code condition
The condition should be if nums[i] != nums[correct_index] to decide when to swap.Step 2: Identify effect of wrong condition
Using == causes swapping only when elements are equal, leading to infinite loops or missed duplicates.Final Answer:
Option B -> Option BQuick Check:
Incorrect condition breaks cyclic sort logic [OK]
Hint: Swap only when elements differ, not when equal [OK]
Common Mistakes:
- Reversing the swap condition causing infinite loops
- Incrementing index incorrectly
- Swapping elements in wrong order
4. Consider the following buggy implementation of the cyclic sort approach to find the smallest missing positive range. Identify the line that contains the subtle bug causing incorrect results or infinite loops.
def find_smallest_missing_range(nums):
n = len(nums)
i = 0
while i < n:
correct_pos = nums[i] - 1
if 1 <= nums[i] <= n and nums[i] != nums[correct_pos]:
nums[i], nums[correct_pos] = nums[correct_pos], nums[i]
# Missing increment of i here
else:
i += 1
start = 1
for i in range(n):
if nums[i] != i + 1:
start = i + 1
break
else:
return [n + 1, n + 1]
end = start
while end <= n and nums[end - 1] != end:
end += 1
return [start, end - 1]
medium
Solution
Step 1: Identify the infinite loop cause
After swapping, the index i is not incremented, so the same index is checked repeatedly causing infinite loop.Step 2: Confirm other lines are correct
Swap line correctly swaps elements; the for loop and return lines handle missing range detection properly.Final Answer:
Option A -> Option AQuick Check:
Missing i increment after swap causes infinite loop or incorrect behavior [OK]
Hint: Always increment index after swap or carefully control loop [OK]
Common Mistakes:
- Forgetting to increment i after swap
- Misplacing swap condition
5. The following code attempts to find the kth missing positive number using binary search. Which line contains a subtle bug that causes incorrect results when the kth missing number is less than the first element of the array?
medium
Solution
Step 1: Identify the bug in condition
The condition uses <= instead of < when comparing missing and k. This causes the binary search to move low pointer incorrectly when missing equals k.Step 2: Explain impact on edge cases
When kth missing is less than arr[0], missing counts start at zero, and using <= causes the search to skip the correct boundary, returning wrong results.Final Answer:
Option A -> Option AQuick Check:
Changing <= to < fixes boundary handling [OK]
Hint: Check strict inequality in binary search condition [OK]
Common Mistakes:
- Using <= instead of <
- Incorrect return formula
