Practice
max_gap after the second iteration of the for-loop when the input is [3, 6, 9, 1]?
from typing import List
def maximumGap(nums: List[int]) -> int:
if len(nums) < 2:
return 0
min_num, max_num = min(nums), max(nums)
if min_num == max_num:
return 0
nums.sort()
max_gap = 0
for i in range(1, len(nums)):
max_gap = max(max_gap, nums[i] - nums[i-1])
return max_gap
print(maximumGap([3,6,9,1])) # Output: ?
Solution
Step 1: Sort the input array
Input: [3,6,9,1] -> Sorted: [1,3,6,9]Step 2: Trace max_gap updates in the loop
Iteration 1 (i=1): max_gap = max(0, 3-1) = 2 Iteration 2 (i=2): max_gap = max(2, 6-3) = 3Final Answer:
Option A -> Option AQuick Check:
max_gap after second iteration is 3 [OK]
- Off-by-one errors in loop
- Using unsorted array differences
- Confusing iteration indices
[4, 2, 1, 3]?
def minimum_abs_difference(arr):
arr.sort()
min_diff = float('inf')
for i in range(len(arr) - 1):
diff = arr[i + 1] - arr[i]
if diff == 1:
return 1
if diff < min_diff:
min_diff = diff
return min_diff
print(minimum_abs_difference([4, 2, 1, 3]))
Solution
Step 1: Sort the array
Sorted array is [1, 2, 3, 4].Step 2: Iterate and check adjacent differences
Differences: 2-1=1 -> triggers early return 1.Final Answer:
Option A -> Option AQuick Check:
Early exit on diff==1 returns 1 [OK]
- Forgetting early exit
- Returning difference between non-adjacent pairs
Solution
Step 1: Understand the problem constraints
The problem requires sorting three distinct values in a single pass with O(1) extra space.Step 2: Identify the algorithm that meets these constraints
The Dutch National Flag algorithm uses three pointers to partition the array in one pass without extra space.Final Answer:
Option B -> Option BQuick Check:
One-pass, constant space sorting of three distinct values matches Dutch National Flag algorithm [OK]
- Thinking counting sort is optimal despite multiple passes
- Assuming comparison-based sorts are needed
- Confusing DP with sorting problems
nums = [-4, -2, 2, 4], a = 1, b = 3, c = 5, what is the final returned array?Solution
Step 1: Compute transformed values
For nums = [-4, -2, 2, 4], quad(x) = x² + 3x + 5: -4 -> 16 -12 + 5 = 9 -2 -> 4 -6 + 5 = 3 2 -> 4 + 6 + 5 = 15 4 -> 16 + 12 + 5 = 33Step 2: Two pointers fill from end
a >= 0, so fill result from end: Compare left_val=9 and right_val=33 -> 33 > 9, place 33 at result[3] Move right pointer left Next compare 9 and 15 -> 15 > 9, place 15 at result[2] Move right pointer left Next compare 9 and 3 -> 9 > 3, place 9 at result[1] Move left pointer right Finally place 3 at result[0] Result array is [3, 9, 15, 33]Final Answer:
Option C -> Option CQuick Check:
Output is sorted ascending: [3, 9, 15, 33] [OK]
- Confusing index direction for filling output
- Mixing up left and right pointer values
nums[0] < nums[1] > nums[2] < nums[3] > nums[4] and so on, ensuring a "wiggle" pattern. Which approach guarantees an optimal solution that runs in O(n log n) time and uses O(1) extra space?Solution
Step 1: Understand the wiggle property requirements
The problem requires elements to be reordered so that nums[0] < nums[1] > nums[2] < nums[3] > nums[4]..., which is not guaranteed by simple sorting or greedy swaps.Step 2: Identify the approach that ensures correct interleaving
Sorting and then using the median with virtual indexing and three-way partitioning ensures elements greater than median are placed at odd indices and smaller at even indices, maintaining the wiggle property in O(n log n) time and O(1) space.Final Answer:
Option B -> Option BQuick Check:
Virtual indexing and median partitioning guarantee wiggle order [OK]
- Assuming greedy swaps fix all cases
- Using DP for rearrangement instead of subsequence
- Ignoring in-place rearrangement for space efficiency
