Largest Rectangle in Histogram
Updated
Problem statement
heights describes a histogram. Bar i is 1 unit wide and heights[i] units tall, all bars stand on the same baseline, and each bar touches the next one. Find the largest rectangle that stands on the baseline and lies completely inside the bars, and return its area.
The sides of such a rectangle follow the bar edges, so it covers one bar or several bars in a row, and it can be no taller than the shortest bar it covers. A height can be 0; a rectangle with a positive area cannot cover a bar of height 0.
Function
largestRectangleArea(heights: int[]) → intExamples
Example 1
heights = [3,7,8,4,1,6]return = 14The bars of height 7 and 8 stand next to each other, so a rectangle of height 7 fits over both: area 7 * 2 = 14. Going wider costs height: height 4 over the bars 7, 8, 4 gives 12, and height 3 over the first four bars also gives 12. No other choice beats 14.
Example 2
heights = [4,2,0,3,2,5]return = 6The bar of height 0 blocks every rectangle that would cross it. To its right, height 2 fits over the bars 3, 2, 5: area 2 * 3 = 6. To its left the best is 4 (the first bar alone, or height 2 over two bars).
Example 3
heights = [1,2,3,4,5]return = 9The heights only grow, so a rectangle of height h can cover every bar from the one of height h to the end. The areas are 1 * 5 = 5, 2 * 4 = 8, 3 * 3 = 9, 4 * 2 = 8 and 5 * 1 = 5, so the answer is 9.
Constraints
1 <= heights.length <= 10^50 <= heights[i] <= 10^4
More ByteDance Phone Screen questions
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Implement QuicksortPHONE SCREEN · Seen Dec 2026
- Reverse Linked ListPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Count N-Queens PlacementsPHONE SCREEN · Seen Nov 2026
- Matrix EscapePHONE SCREEN · Seen Nov 2026
- Largest Number Below N from Reusable DigitsPHONE SCREEN · Seen Nov 2026
- Log Analyzer: Busiest Service in a Rolling WindowPHONE SCREEN · Seen Nov 2026
- Majority ElementPHONE SCREEN · Seen Nov 2026
- Valid ParenthesesPHONE SCREEN · Seen Nov 2026
- Binary Tree Target-Sum PathsPHONE SCREEN · Seen Nov 2026
Similar questions at other companies
- Trapping Rain WaterTeradata · ONSITE INTERVIEW
- Trapping Rain WaterBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Count Visible People to the LeftGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Construct a Tree from Level-Order and Inorder TraversalsSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Largest Rectangle in HistogramTekion · ONSITE INTERVIEWTekion interview questions
- Trapping Rain WaterAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Trapping Rain WaterKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Confirmed Profiling Events with First-Observed StartsAnthropic · PHONE SCREEN · ONSITE INTERVIEWAnthropic interview questions
- Trapping Rain WaterNuro · ONSITE INTERVIEWNuro interview questions
- Largest Rectangle in HistogramAirwallex · PHONE SCREENAirwallex interview questions
ByteDance interview experiences
- ByteDance TikTok USDS SWE Intern Interview: 1 Experience
- ByteDance MLE New Grad Interviews: 1 Experience
- ByteDance Security SRE Intern Phone Screens: 1 Experience
- ByteDance USDS New Grad SWE Phone Screen: 1 Experience
- ByteDance New Grad Data Engineer Video Interviews: 1 Experience
- ByteDance Trust & Safety Research Scientist Intern: 1 Experience