ExpediaMaximum Hole Area After Removing Prison Bars
A grid is formed by n horizontal and m vertical bars; given the indices of horizontal and vertical bars to remove, return the area of the largest hole after removal. Key subtlety: the given numbers index the bars between cells, not the cells themselves. Sort each direction's removed indices, find the longest run of consecutive removed bars per direction, and multiply the two resulting gap sizes.
Problem
A prison window is made of n horizontal bars (numbered 1..n) and m vertical bars (numbered 1..m) arranged in a grid. Some bars are removed; return the area of the largest rectangular hole left after all removals.
- Input:
n,m, the listhBarsof horizontal bar indices to remove, and the listvBarsof vertical bar indices to remove. - The numbers are bar indices, i.e. the lines between cells, not cell indices.
- Duplicates in a list count as removing that bar once.
- After coding, the interviewer asks for time and space complexity.
Input format (stdin):
n m p q
hBars[0..p-1] # empty line if p = 0
vBars[0..q-1] # empty line if q = 0Constraints:
1 <= n, m <= 1e9
0 <= p, q <= 2e5
1 <= hBars[i] <= n; 1 <= vBars[i] <= mKey fact: if the longest run of consecutive removed bars in one direction has length k, the hole length in that direction is k + 1. So the answer is (max horizontal run + 1) * (max vertical run + 1).
Examples
n=3, m=3, hBars=[1], vBars=[2]->(1+1) * (1+1) = 4n=6, m=6, hBars=[2,3,4], vBars=[3,4]-> longest runs are 3 and 2 ->(3+1) * (2+1) = 12
Hints
Pitfalls
- Treating indices as cells instead of bars gives off-by-one answers; the
+1is the most common slip. - Forgetting to deduplicate: duplicates must count once, or runs are inflated.
- Mishandling empty lists: when
p = 0orq = 0the corresponding input line is empty, and the run is 0 (dimension 1), not an error. - One 50-minute slot is spent on this single problem. The platform ships with 13 prebuilt test cases, so you need no harness of your own, but every case has to pass.
- Interviewers are friendly and give hints when you stall; no follow-up was asked beyond complexity. A short BQ segment (recent projects, motivation for Expedia) can share the same screen.
Talking Points
- State complexity in terms of
pandq, notnandm: the algorithm does not depend on the window size, only on the number of removed bars. - Explain the
+1carefully: the removed bars plus the gap up to the next kept bar (or the edge) givek + 1unit segments. - Handle duplicates explicitly, via
set()or by skipping equal neighbors after sorting. - Walk an example whose run touches the boundary (bars
1, 2, 3removed gives dimension 4) to show the formula still holds. - The maximum answer is about
(1e9 + 1)^2, roughly1e18, which overflows 32-bit integers; Python integers are unbounded.
Follow-ups
Preparation
- Draw a 4x4 bar grid on paper, remove two adjacent horizontal bars and one vertical bar, and count the resulting hole by hand to fix the index model in your head.
- Implement the solution in under 15 minutes: sort each removed-index list, scan for the longest run of consecutive integers, convert run length to gap size, multiply.
- Test edge cases out loud: no bars removed in one direction, all bars removed, unsorted / duplicate-free input with gaps of one.
