Practice
[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
arr1 = [3,1,2,2] and arr2 = [2,3], what is the final returned list?Solution
Step 1: Build frequency array for arr1
arr1 = [3,1,2,2], max_val=3, freq = [0,1,2,1] (indices 0 to 3)Step 2: Construct result using arr2 order then leftovers
Output elements for arr2: 2 appears twice -> [2,2], 3 appears once -> [2,2,3]. Leftover is 1 once -> [2,2,3,1]Final Answer:
Option C -> Option CQuick Check:
Frequency counts and order match output [OK]
- Appending leftover elements before arr2 order
- Miscounting frequencies or off-by-one in freq array
- Mixing order of arr2 elements in output
from typing import List
import collections
import heapq
def diagonalSort(mat: List[List[int]]) -> List[List[int]]:
m, n = len(mat), len(mat[0])
diagonals = collections.defaultdict(list)
for i in range(m):
for j in range(n):
heapq.heappush(diagonals[i - j], mat[i][j])
for i in range(m):
for j in range(n):
mat[i][j] = heapq.heappop(diagonals[i - j])
return mat
matrix = [[3,3,1,1],[2,2,1,2],[1,1,1,2]]
print(diagonalSort(matrix))Solution
Step 1: Build min-heaps keyed by diagonal (row - col)
For each element, push into heap keyed by i - j. For example, diagonal key 0 has elements [3,2,1], which heapify to [1,2,3].Step 2: Pop from heaps to replace matrix elements
Traverse matrix again, replacing each element with the smallest from its diagonal heap. This sorts each diagonal ascending.Final Answer:
Option A -> Option AQuick Check:
Diagonal keys and heaps produce sorted diagonals matching [[1,1,1,1],[1,2,2,2],[1,2,3,3]] [OK]
- Off-by-one errors in heap keys
- Mixing up heap push/pop order
- Incorrectly reconstructing matrix from heaps
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
Solution
Step 1: Analyze binary search calls per iteration
Each iteration advances pointer to next missing range start or after current number. Each bisect call is O(log n). Since pointer can advance by 1 in worst case, total iterations can be up to (upper - lower + 1).Step 2: Combine iteration count and bisect cost
In worst case, pointer advances one by one through the entire range, each bisect O(log n), total O((upper - lower) * log n).Final Answer:
Option B -> Option BQuick Check:
Binary search per number in range leads to O((upper - lower) log n) [OK]
- Assuming pointer always skips ahead by elements
- Confusing n and range size
- Ignoring worst-case pointer increments
