Practice
k closest elements to x in a sorted array. What is the returned output when calling findClosestElements([1, 3, 5], 2, 4)?Solution
Step 1: Initialize variables
Array length is 3, k=2, solow=0,high=1.Step 2: First iteration of binary search
Calculatemid = (0+1)//2 = 0. Comparex - arr[mid] = 4 - 1 = 3andarr[mid + k] - x = arr[2] - 4 = 5 - 4 = 1. Since 3 > 1, setlow = mid + 1 = 1.Step 3: Loop ends as
Returnlow == high == 1arr[1:3] = [3, 5].Final Answer:
Option D -> Option DQuick Check:
Binary search correctly chooses window starting at index 1 [OK]
- Off-by-one error in slicing window
- Incorrect comparison direction in if condition
- Returning wrong slice boundaries
Solution
Step 1: Understand problem constraints
The array is rotated and may contain duplicates, which complicates the binary search because duplicates can hide the pivot.Step 2: Identify suitable algorithm
Modified binary search that shrinks the search space when duplicates are encountered ensures correctness and efficiency on average.Final Answer:
Option D -> Option DQuick Check:
Modified binary search handles duplicates correctly [OK]
- Assuming simple binary search works without handling duplicates
- Using greedy or DP unnecessarily
n x n matrix where each row and each column is sorted in ascending order. You need to find the kth smallest element in the matrix efficiently. Which of the following approaches guarantees the optimal time complexity for this problem?Solution
Step 1: Understand problem constraints
The matrix rows and columns are sorted, so each row is sorted and each column is sorted independently.Step 2: Identify the optimal approach
Using a min-heap to merge sorted rows efficiently extracts the smallest elements in ascending order, achieving O(k log n) time, better than flattening and sorting.Final Answer:
Option D -> Option DQuick Check:
Min-heap merges sorted lists optimally [OK]
- Assuming flatten and sort is optimal
- Confusing DP with this problem
- Thinking greedy without data structures works
Solution
Step 1: Identify prev index calculation
Prev is set to mid - 1 without modulo, which can be -1 if mid=0, causing out-of-bound or incorrect access.Step 2: Correct calculation uses modulo to wrap around
Prev should be (mid - 1 + n) % n to handle circular indexing safely.Final Answer:
Option B -> Option BQuick Check:
Prev index must wrap around to avoid negative index [OK]
- Forgetting modulo on prev index
- Assuming mid > 0 always
- Ignoring circular array property
Solution
Step 1: Understand requirement for leftmost insertion
We must find the earliest index where target can be inserted, including duplicates.Step 2: Modify binary search condition
If nums[mid] >= target, recurse left to find earlier occurrence; else recurse right.Step 3: Avoid linear scan for efficiency
Linear scan after binary search defeats O(log n) goal.Final Answer:
Option A -> Option AQuick Check:
Condition ensures leftmost insertion index found efficiently [OK]
- Recursing right on >= target misses leftmost index
- Returning right instead of left at base case
- Using linear scan after binary search
