Sorted event times: [2,4,4,9]. Find the first position at least 4.
A midpoint sees the second 4. Decision: Which entire region is now certified, and why can’t equality return yet?
1 / 12
Search Insert Position — predict before coding
Search for a boundary, not a lucky equal element. The desired index is the first position whose value is at least target, which also handles missing targets and duplicates consistently.
Decision: For [1,3,3,7] and target 3, what is the insertion index? Why is returning the first equal midpoint insufficient?
2 / 12
Search a 2D Matrix — predict before coding
Row-major order is globally sorted only because the next row starts above the previous row’s last element. This stronger promise permits one binary search over virtual flattened indices.
Decision: A virtual search inspects flat index 4 in a matrix with two rows and three columns. Give its coordinates. If both rows are sorted but the first ends at 10 and the second begins at 7, can that coordinate mapping justify the binary-search discard?
3 / 12
Find Peak Element — predict before coding
The array itself need not be monotone. Comparing a midpoint to its right neighbor gives a direction guaranteed to contain some peak, not necessarily the global maximum.
Decision: For [9,7,5,3], complete the slope-search intervals from [0,3]. What certifies an endpoint peak? Now permit equal adjacent values: would an all-equal array still guarantee a strict peak?
4 / 12
Search in Rotated Sorted Array — predict before coding
Rotation leaves at least one half around the midpoint sorted when values are distinct. Determine which half is ordered before deciding whether the target belongs inside its value range.
Decision: Compare [1,1,1,3,1] and [1,3,1,1,1] while searching for 3 with lo=0, hi=4, mid=2. Both have the same values at lo, mid, and hi. Can the comparison nums[lo]<=nums[mid] justify discarding the left half in both? Explain which original precondition supplied the missing evidence.
5 / 12
Find First and Last Position of Element in Sorted Array — predict before coding
An equality range is the difference between two boundaries: first value >=target and first value >target. Two logarithmic searches avoid a linear scan through a long duplicate run.
Decision: A lower-bound search returns len(nums) for a missing target larger than every value. A learner immediately evaluates nums[left] to check equality. What guard order is required, and what changes when the input itself is empty?
6 / 12
Find Minimum in Rotated Sorted Array — predict before coding
The right endpoint identifies which side of the rotation contains the minimum. A midpoint greater than that endpoint lies in the high segment before the minimum.
Decision: Change the promise to allow duplicates. On [2,2,2,0,1,2], midpoint and right endpoint are both 2. Why is the original hi=mid branch unsafe, and what can safely be discarded when equality supplies no direction?
7 / 12
Median of Two Sorted Arrays — predict before coding
Take i values from smaller A and j=ceil((m+n)/2)-i from B.
Search cuts 0 through m, including both empty-side boundaries.
Decision: A proposed odd-length median partition puts floor((m+n)/2) values on the left but still returns the maximum left value. Find its result on A=[0], B=[2,4], then repair the rank convention.
8 / 12
Median: prove both cut directions
Violation, when both entries exist
Cut update
A[i-1] > B[j]
hi=i-1: fewer A values on the left
B[j-1] > A[i]
lo=i+1: more A values on the left
Decision: A=[5,6], B=[1,2,3,4], i=1, j=2. Why can every larger i also be discarded?
9 / 12
Median: read only existing boundaries
At i=m, A has no right boundary. At i=0, A has no left boundary.
Odd total: maximum existing left value.
Even total: mean of maximum left and minimum right.
Decision: With A=[] and B=[2,6], which values supply the median? Is a fabricated infinity needed?
10 / 12
Change the requested boundary
For [-6,-2,-2,3,8], return the last index with value at most -2.
Derive the equality update. Also handle thresholds -7 and 8 without threshold+1.
11 / 12
Reconstruct and transfer
Write what is already known outside the interval, what remains inside, and why each update shrinks.
Test endpoint arithmetic and absent partition boundaries.
12 / 12
←/→ or click edges to navigate · ? help · N notes · F fullscreen