← Questions
Fewest boards to cover every hole in a roof
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Google's first round (coding), last asked Feb 2026. It tests greedy and sorting.
A roof has holes at integer positions along a line. You have as many boards as you like, each of fixed length L: a board placed at position p covers positions p to p + L - 1. Return the fewest boards needed so every hole is covered. Boards may overlap.
Interface
Unless you agree on a different interface with the interviewer, assume:
min_boards(holes: list[int], L: int) -> int
Example
holes = [1, 2, 5, 9, 10], L = 4 -> 3 # boards at 1 (1-4), 5 (5-8), 9 (9-12)
holes = [5, 1, 2], L = 10 -> 1
holes = [], L = 3 -> 0
Constraints
- Up to 10^5 holes; positions up to 10^9; positions may repeat and arrive unsorted.
-
L >= 1.
Clarifying questions
- Are positions integers, and does a board of length
LcoverLpositions? - Can boards overlap or hang past the roof's edge?
- Is the roof one line, or a 2D grid of cells?
What a strong answer covers
- Sort the holes; put a board's left end on the leftmost uncovered hole, skip every hole it covers, repeat.
- The exchange argument: any optimal solution's first board can be shifted right to start at the first hole without uncovering anything, so the greedy choice is safe.
- O(n log n) for the sort and O(n) for the sweep; duplicates handled for free.
- Tests: empty input, all holes under one board, holes exactly
Lapart.
Follow-ups
- The roof is a 2D grid of cells and boards are
1 x L, laid along rows only. Then along rows or columns: why does that become much harder? - Boards come in several lengths with different costs: minimize total cost.
- You have only
kboards: what is the shortest length that covers every hole?
Where it's asked
- Google · First round: coding · Last asked Feb 202645 min · Coding in a Google Doc, no syntax highlighting or autocomplete · No AI
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
