Skip to content
Preptima

Sorting & Searching interview questions

Binary search on answers, custom comparators, and sorting internals still asked about.

4 questions in Data Structures & Algorithms.

medium4

mediumCoding

Shipping packages within D days minimises a maximum. Now place m sensors at fixed positions so the closest pair is as far apart as possible. What changes in the binary search?

It is still binary search on the answer, over the gap rather than the positions, but the predicate flips. A gap that works means every smaller gap works, so you want the last feasible value, which needs lo = mid with a ceiling midpoint. Sort first, check each gap greedily, and the cost is O(n log n + n log R).

5 minmid, senior
mediumCoding

Given package weights in a fixed order, find the smallest ship capacity that delivers them all within D days

When you cannot construct the answer but can cheaply test a candidate, and that test is monotone, binary search the answer's numeric range instead of the input. The feasibility check here is greedy day-packing, giving O(n log S) time, and the whole implementation hinges on one loop invariant. It also connects monotonicity to the point an interviewer is testing.

5 minmid, senior, staff