Practice
1. Given the following code snippet, what is the output when calling
findErrorNums([1, 2, 2, 4])?easy
Solution
Step 1: Trace the cyclic sort swaps
Initial array: [1, 2, 2, 4]. The duplicate 2 causes a swap to place numbers correctly except index 2 which remains 2.Step 2: Identify mismatch after sorting
After sorting, array is [1, 2, 2, 4]. At index 2, expected 3 but found 2 -> duplicate=2, missing=3.Final Answer:
Option C -> Option CQuick Check:
Output matches expected duplicate and missing pair [OK]
Hint: Mismatch found where index+1 != value after cyclic sort [OK]
Common Mistakes:
- Confusing duplicate and missing order
- Off-by-one errors in indexing
- Returning wrong pair from loop
2. 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
3. What is the time complexity of the optimal cyclic sort approach for finding the smallest missing positive range in an array of length n, and why might some candidates mistakenly think it is higher?
medium
Solution
Step 1: Analyze the swapping behavior
Each element is swapped at most once to its correct index, so total swaps ≤ n.Step 2: Understand loop iterations
The outer while loop increments i only when no swap occurs, ensuring linear total iterations.Final Answer:
Option B -> Option BQuick Check:
Linear time due to bounded swaps and single pass [OK]
Hint: Each element moves at most once -> O(n) time [OK]
Common Mistakes:
- Assuming swaps cause O(n^2)
- Confusing sorting complexity with cyclic sort
4. 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
5. Suppose the input array can contain numbers outside the range 1 to n, including negatives and zeros, and duplicates. Which modification to the cyclic sort algorithm ensures correct placement of numbers 1 to n at indices 0 to n-1 while ignoring invalid numbers?
hard
Solution
Step 1: Understand input constraints
Input may have invalid numbers (negatives, zeros, > n) and duplicates that break in-place cyclic sort.Step 2: Use extra array for placement
Creating a separate array of size n and placing valid numbers at index num-1 ensures correct placement ignoring invalids and duplicates.Step 3: Avoid in-place swaps that cause index errors or infinite loops
Extra space approach is safer and simpler for this variant.Final Answer:
Option B -> Option BQuick Check:
Extra array handles invalid and duplicate numbers cleanly [OK]
Hint: Extra array avoids invalid index and duplicate issues [OK]
Common Mistakes:
- Ignoring invalid range checks
- Trying to fix in-place swaps with invalid numbers
- Not handling duplicates properly
