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 coding interview question asked at Microsoft and Meta. It tests sorting, binary-search and greedy.
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.
Clarifying questions
- If one board can cover every hole with length 0 (all holes at one point), is the answer
1, sinceLmust be positive? - Can the two boards overlap or sit at the same place? (Yes.)
- Is a repeated hole position one hole or two? (It does not change the answer.)
- Should we return where to place the boards?
What a strong answer covers
- Sorts the holes. Some optimal pair of boards covers a prefix and a suffix of the sorted list.
- For each split point, the length needed is the larger span of the two parts. The answer is the smallest such value, but at least 1. That is O(n log n) for the sort and O(n) for the scan.
- Or binary searches on
Lwith a greedy check, and says why the split scan is simpler. - Tests both examples, a single hole, all holes equal, two holes far apart, and negative positions.
Follow-ups
- Use
kboards: binary search onLwith a greedy check. - The two boards may have different lengths, and we minimize their sum.
- Return the board positions.
- Holes on a 2D table, with square patches.
Where it's asked
What it tests
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.

