Postcode Stretch on the Van
A delivery van's parcels sit on one long rack, sorted by postcode. Parcels sharing a postcode form a stretch of neighbours.
When the driver enters a postcode area, the tablet lights up that stretch by showing the first and last parcel for the postcode, or nothing if there are none.
Your task
Write findStretch(postcodes, code). postcodes is sorted in non-decreasing order. Return [first, last], where first is the smallest index holding code and last is the largest. If code is not present (or the list is empty), return [-1, -1].
Aim for O(log n) even when one postcode fills most of the rack.
findStretch(postcodes: int[], code: int) → int[]
Examples
postcodes = [2040, 2040, 3115, 3115, 3115, 3120, 4500] code = 3115
[2, 4]
Postcode 3115 fills indexes 2, 3 and 4, while index 1 holds 2040 and index 5 holds 3120.
postcodes = [2040, 2040, 3115, 3115, 3115, 3120, 4500] code = 3200
[-1, -1]
The postcode 3200 would sit between 3120 and 4500, but no parcel has it.
Constraints
- 0 ≤ postcodes.length ≤ 100,000
- 1000 ≤ postcodes[i], code ≤ 99,999
- postcodes is sorted in non-decreasing order.
Start with brute force
One approach is to scan the rack for the first and last match. Another is to binary search for any match and then walk left and right to the edges.
Both are O(n) in the worst case: if 99,000 of 100,000 parcels share the postcode, the walk visits nearly all of them. Two boundary searches need about 34 comparisons in total, however long the stretch is.
Hints
0 of 4 shown- Hint 1
A normal binary search for 3115 in the first example might land on index 3. Does that tell you where the stretch starts and ends?
- Hint 2
You could walk outwards from index 3, but if 90,000 parcels share the postcode, that is slow. You need the two edges directly.
- Hint 3
The stretch starts at the first index with a value of at least
code, and ends one before the first index with a value greater thancode. Both are "first index where a condition becomes true". - Hint 4
Write one helper
firstIndex(cond)with the lower-bound loop (lo = 0,hi = n,hi = midwhen true,lo = mid + 1otherwise). Call it forvalue >= codeand forvalue > code. If the first result is out of range or does not holdcode, return[-1, -1].
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
Instead of searching for the postcode itself, find the two edges of its stretch, each with its own binary search.
Trace [2040, 2040, 3115, 3115, 3115, 3120, 4500] with code 3115.
First search, condition v >= 3115:
- With
lo = 0,hi = 7andmid = 3, the value 3115 qualifies, sohi = 3. - With
lo = 0,hi = 3andmid = 1, the value 2040 does not qualify, solo = 2. - With
lo = 2,hi = 3andmid = 2, the value 3115 qualifies, sohi = 2. The loop stops withfirst = 2, and index 2 holds 3115.
Second search, condition v > 3115:
- With
lo = 0,hi = 7andmid = 3, the value 3115 does not qualify, solo = 4. - With
lo = 4,hi = 7andmid = 5, the value 3120 qualifies, sohi = 5. - With
lo = 4,hi = 5andmid = 4, the value 3115 does not qualify, solo = 5. The loop stops, and the stretch ends at 5 − 1 = 4.
The answer is [2, 4]. Do not skip the check after the first search: when the code is missing, it still returns the position where the code would go, so confirm that position holds it.
function findStretch(postcodes, code) {
// first index where isTrue(value) holds, or n if none
const firstIndex = (isTrue) => {
let lo = 0, hi = postcodes.length;
while (lo < hi) {
const mid = Math.floor((lo + hi) / 2);
if (isTrue(postcodes[mid])) hi = mid;
else lo = mid + 1;
}
return lo;
};
const first = firstIndex((v) => v >= code);
if (first === postcodes.length || postcodes[first] !== code) return [-1, -1];
const afterLast = firstIndex((v) => v > code);
return [first, afterLast - 1];
}def find_stretch(postcodes, code):
# first index where is_true(value) holds, or n if none
def first_index(is_true):
lo, hi = 0, len(postcodes)
while lo < hi:
mid = (lo + hi) // 2
if is_true(postcodes[mid]):
hi = mid
else:
lo = mid + 1
return lo
first = first_index(lambda v: v >= code)
if first == len(postcodes) or postcodes[first] != code:
return [-1, -1]
after_last = first_index(lambda v: v > code)
return [first, after_last - 1]Run your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.