Shortest equal length for two boards covering every hole
I was asked this
No reports yet. Sign in to say where you were asked it.
A table top has holes at integer positions along a single line, given as an unsorted array holes (positions may repeat or be negative). You will patch it with exactly two boards that must have the same positive integer length L. A board placed at position X covers every hole whose position is in [X, X + L], both ends included. Boards may be placed anywhere and may overlap.
Implement min_board_length(holes: list[int]) -> int that returns the smallest L for which all holes can be covered.
Constraints
1 <= len(holes) <= 10^5-10^9 <= holes[i] <= 10^9- Aim for O(n log n) time.
Example 1
holes = [3, 9, 4]
output: 1
One board covers [3, 4], the other covers [9, 10].
Example 2
holes = [0, 4, 5, 9, 10, 14]
output: 5
Boards at [0, 5] and [9, 14]. Splitting at another gap of 4 would leave one board needing length 10.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
Sign in to submit your code; every attempt is kept here.

