DSA interview question
A highway authority has gas stations at sorted positions along a road and budget for K more. New stations can go anywhere, including at fractional positions. Where should they go so that the largest gap between neighbouring stations is as small as possible? The answer is a real number. Greedily splitting the currently largest gap works with a heap, but the cleaner technique is binary search on the real-valued answer. For a target gap d, each existing gap g needs ceil(g / d) − 1 new stations, and the target is feasible if the total is at most K.
Example: stations [1, 2, ..., 10], K = 9 → 0.5 (one new station in the middle of every gap).
double minimiseMaxDistance(const vector<int>& stations, int k); // sorted positions, answer within 1e-6
Input (stations, k) | Output | Explanation |
|---|---|---|
([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 9) | 0.500000 | Halve every gap of 1 |
([23, 24, 36, 39, 46, 56, 57, 65, 84, 98], 1) | 14.000000 | Split the 19-wide gap; the 14-wide gap remains the largest |
([1, 2], 1) | 0.500000 | |
([1, 13, 17, 23], 5) | 3.000000 | The 12-gap takes 3 stations, the 4-gap 1, the 6-gap 1 |
(Confirm: stations are sorted and distinct; new stations can be placed at any real position; the answer is accepted within 10⁻⁶.)
On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in