Practice
1. You are given an unsorted array of integers that may contain duplicates and negative numbers. Your task is to find the first
k positive integers missing from the array. Which algorithmic approach guarantees an optimal time complexity while handling duplicates and negatives correctly?easy
Solution
Step 1: Understand problem constraints
The array may contain duplicates and negative numbers, so naive sorting or heap approaches may be inefficient or incorrect.Step 2: Identify optimal approach
Cyclic sort places numbers in their correct indices in O(n) time, ignoring negatives and duplicates. Then, a boolean array tracks extra numbers beyond the array length to find missing positives efficiently.Final Answer:
Option C -> Option CQuick Check:
Cyclic sort + boolean array handles duplicates and negatives optimally [OK]
Hint: Cyclic sort handles duplicates and negatives efficiently [OK]
Common Mistakes:
- Assuming sorting is always optimal
- Using heaps which add unnecessary overhead
- Confusing DP with presence tracking
2. You are given an unsorted array of integers that may contain duplicates, negatives, and zeros. Your task is to find the smallest continuous range of positive integers that are missing from the array. Which algorithmic approach guarantees an optimal O(n) time and O(1) space solution for this problem?
easy
Solution
Step 1: Understand problem constraints
The problem requires finding the smallest missing positive range in O(n) time and O(1) space, which excludes sorting (O(n log n)) and hash sets (O(n) space).Step 2: Identify the algorithm that achieves in-place linear time
Cyclic sort places each positive integer at its correct index in the array, enabling a single pass to find the missing range efficiently.Final Answer:
Option D -> Option DQuick Check:
Cyclic sort is known for in-place linear time sorting of numbers in range 1 to n [OK]
Hint: Cyclic sort achieves O(n) time and O(1) space for this problem [OK]
Common Mistakes:
- Assuming sorting is optimal
- Confusing hash set space with time complexity
3. You are given an unsorted integer array and need to find the smallest positive integer missing from it in O(n) time and O(1) extra space. Which algorithmic approach best guarantees this optimal solution?
easy
Solution
Step 1: Understand the problem constraints
The problem requires O(n) time and O(1) space, which rules out sorting (O(n log n)) and hash sets (O(n) space).Step 2: Identify the approach that rearranges elements in-place
Cyclic sort places each number in its correct index (number k at index k-1), enabling a linear scan to find the first missing positive efficiently.Final Answer:
Option D -> Option DQuick Check:
Cyclic sort achieves O(n) time and O(1) space [OK]
Hint: Optimal solution uses in-place cyclic sort [OK]
Common Mistakes:
- Choosing sorting which is O(n log n)
- Using hash sets which use extra space
- Greedy approaches that don't guarantee correctness
4. Identify the bug in the following cyclic sort implementation for finding the corrupt pair:
medium
Solution
Step 1: Analyze the while loop behavior
Without incrementing i in the else branch, the loop can get stuck when nums[i] == nums[correct_index], causing infinite loop.Step 2: Identify fix
Addingi += 1in the else branch ensures progress and termination.Final Answer:
Option A -> Option AQuick Check:
Infinite loop caused by missing index increment [OK]
Hint: Always increment index if no swap to avoid infinite loops [OK]
Common Mistakes:
- Forgetting to increment index after no swap
- Swapping incorrectly
- Misplaced return statements
5. Consider this buggy code snippet for finding the duplicate and missing numbers. Which line contains the subtle bug that can cause an infinite loop or incorrect result?
medium
Solution
Step 1: Analyze the swapping condition
Without checking if nums[i] == nums[correct], swapping duplicates causes infinite loops or incorrect swaps.Step 2: Confirm other lines are correct
Initialization, increment, and return lines are standard and not buggy here.Final Answer:
Option A -> Option AQuick Check:
Missing conditional check before swap causes infinite loop [OK]
Hint: Always check if swap is needed to avoid infinite loops [OK]
Common Mistakes:
- Forgetting to check for duplicates before swapping
- Misplacing increment of index i
- Returning wrong values after loop
