Practice
def cyclic_sort_buggy(arr):
i = 0
n = len(arr)
while i < n:
correct_index = arr[i] - 1
if arr[i] != arr[correct_index]:
arr[i], arr[correct_index] = arr[correct_index], arr[i]
i += 1
return arr
What is the issue causing incorrect output or infinite loops?Solution
Step 1: Analyze loop increment
i is incremented unconditionally even after swapping, skipping re-check of swapped element.Step 2: Understand impact
Elements swapped in may still be misplaced but are not checked again, causing incorrect final array or infinite loops.Final Answer:
Option A -> Option AQuick Check:
Increment i only when no swap occurs to ensure correct placement [OK]
- Always incrementing index after swap
- Ignoring index bounds
- Misusing loop constructs
Solution
Step 1: Analyze the while loop behavior
Each swap places at least one element in its correct position, so total swaps ≤ n.Step 2: Understand why time is linear
Since each element is moved at most once, total operations are O(n).Final Answer:
Option C -> Option CQuick Check:
Swapping each element once ensures linear time [OK]
- Assuming swaps cause O(n^2) due to nested loops
- Confusing cyclic sort with comparison-based sorting
- Forgetting that extra space is O(1)
Solution
Step 1: Identify the main loop behavior
The while loop swaps elements to their correct positions. Each swap places at least one element correctly, so total swaps ≤ n.Step 2: Analyze time complexity
Since each element is moved at most once, the total operations are proportional to n, resulting in O(n) time.Final Answer:
Option A -> Option AQuick Check:
Linear time due to limited swaps and single pass [OK]
- Assuming sorting is involved -> O(n log n)
- Mistaking nested loops inside while as O(n^2)
- Forgetting swaps reduce total operations
def missingNumber(nums):
n = len(nums)
i = 0
while i < n:
correct = nums[i]
if nums[i] != nums[correct]: # Bug here
nums[i], nums[correct] = nums[correct], nums[i]
else:
i += 1
for i in range(n):
if nums[i] != i:
return i
return n
What is the bug in this code?Solution
Step 1: Identify the missing boundary check
The condition lacks 'correct < n' check, so if nums[i] == n, nums[correct] causes index out of range.Step 2: Consequences of missing check
Without this, the code may crash or loop infinitely when encountering the number n, which is outside array indices.Final Answer:
Option A -> Option AQuick Check:
Adding 'correct < n' prevents out-of-bounds errors [OK]
- Forgetting boundary check leads to runtime errors
- Misplacing increment causing infinite loops
- Incorrect swap lines that look correct
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]
- Forgetting to check for duplicates before swapping
- Misplacing increment of index i
- Returning wrong values after loop
