Largest Rectangle in a Histogram

Quick Overview

Return the maximum rectangle area formed by consecutive histogram bars using a linear-time monotonic-stack approach and 64-bit arithmetic.

Largest Rectangle in a Histogram

Company: Airwallex

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

# Largest Rectangle in a Histogram Implement `largest_histogram_rectangle(heights: list[int]) -> int`. Each integer is the height of a unit-width histogram bar. Return the largest rectangular area that can be formed from one or more consecutive bars, with the rectangle's base on the histogram baseline. ### Input Domain - `0 <= len(heights) <= 200,000`. - `0 <= heights[i] <= 10^9`. ### Output Rules - A rectangle spanning indices `left` through `right` has height equal to the minimum bar height in that interval and width `right - left + 1`. - Return `0` for an empty input or when all heights are zero. - Return only the exact maximum area; no coordinates are required. ### Constraints - Use signed 64-bit arithmetic for areas. - Target time is `O(n)` and additional space is `O(n)`. ### Examples #### Example 1 Input: `heights = [2,1,5,6,2,3]` Output: `10` #### Example 2 Input: `heights = [2,4]` Output: `4` ```hint Determine when a bar's right boundary becomes known A monotonic structure can retain candidate bars until a shorter height proves that they cannot extend farther right. ```

Quick Answer: Return the maximum rectangle area formed by consecutive histogram bars using a linear-time monotonic-stack approach and 64-bit arithmetic.

|Home/Coding & Algorithms/Airwallex
Airwallex logo
Airwallex
Sep 2, 2026
mediumSoftware EngineerTechnical ScreenCoding & Algorithms
1
0

Largest Rectangle in a Histogram

Implement largest_histogram_rectangle(heights: list[int]) -> int.

Each integer is the height of a unit-width histogram bar. Return the largest rectangular area that can be formed from one or more consecutive bars, with the rectangle's base on the histogram baseline.

Input Domain

  • 0 <= len(heights) <= 200,000 .
  • 0 <= heights[i] <= 10^9 .

Output Rules

  • A rectangle spanning indices left through right has height equal to the minimum bar height in that interval and width right - left + 1 .
  • Return 0 for an empty input or when all heights are zero.
  • Return only the exact maximum area; no coordinates are required.

Constraints

  • Use signed 64-bit arithmetic for areas.
  • Target time is O(n) and additional space is O(n) .

Examples

Example 1

Input: heights = [2,1,5,6,2,3]

Output: 10

Example 2

Input: heights = [2,4]

Output: 4

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...