Find the largest rectangle beneath a histogram
Company: ByteDance
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: Find the maximum rectangle area under adjacent unit-width histogram bars. Handle repeated heights and zero bars, explain the stack invariant, and use arithmetic wide enough for large areas.
Constraints
- `0 <= len(heights) <= 100000`.
- `0 <= heights[i] <= 1000000000`.
- The answer can exceed signed 32-bit range. Use 64-bit arithmetic. The maximum is `100000000000000`, below `2^53`.
Examples
Input: ([2, 1, 5, 6, 2, 3],)
Expected Output: 10
Explanation: A rectangle can be wider than the tallest individual bar.
Input: ([4, 4, 0, 3],)
Expected Output: 8
Explanation: Equal heights combine, but a zero splits positive runs.
Hints
- For a fixed height, the useful span extends until a smaller bar on either side.
- When a shorter bar arrives, some earlier heights can no longer extend to the right.