Largest Rectangle in Histogram

Algorithms
Hard
Uber
35.1K views

Given an array of integers representing the histogram's bar heights, find the area of the largest rectangle in the histogram. Use a Monotonic Stack.

Why Interviewers Ask This

Uber asks this problem to evaluate a candidate's ability to optimize brute-force solutions for real-time logistics systems. They specifically test proficiency with Monotonic Stacks, a critical pattern for finding next/smaller elements efficiently. This assesses whether you can handle O(N) constraints required for high-volume data processing in ride-sharing algorithms.

How to Answer This Question

1. Clarify the problem by defining inputs and outputs, mentioning edge cases like empty arrays or single bars. 2. Propose the naive O(N^2) solution first to show baseline understanding, then immediately pivot to the optimal approach. 3. Explain the Monotonic Stack strategy: maintain indices of increasing heights. When a smaller height is encountered, pop elements to calculate potential areas. 4. Walk through a concrete example, such as [2, 1, 5, 6, 2, 3], tracing stack operations step-by-step. 5. Analyze time complexity (O(N)) and space complexity (O(N)), emphasizing why this meets Uber's scalability requirements. 6. Write clean code with comments explaining the 'extend left' logic when popping from the stack.

Key Points to Cover

  • Demonstrating clear understanding of the Monotonic Stack pattern for Next Smaller Element problems
  • Explicitly stating O(N) time and O(N) space complexity analysis
  • Handling boundary conditions like empty arrays or single elements gracefully
  • Using a sentinel value (0) to force stack cleanup without complex post-loop logic
  • Explaining the logic of calculating width based on stack indices during the pop operation

Sample Answer

To solve the Largest Rectangle in Histogram problem efficiently, I would first acknowledge that a brute-force approach checking every pair of boundaries takes O(N^2), which is too slow for large datasets typical at Uber. Instead, I propose using a Monotonic Increasing Stack to achieve O(N) time complexity. The core idea is to iterate through the histogram bars while maintaining a stack of indices where heights are non-decreasing. Whenever we encounter a bar shorter than the stack's top, we know the bar at the top cannot extend further right. We pop it, calculate its area assuming it extends left to the new top of the stack minus one, and update our maximum. For example, with input [2, 1, 5, 6, 2, 3], when we hit the second '2', the '6' and '5' are popped because they are taller. We calculate their max areas immediately. Finally, we process remaining items in the stack after appending a zero-height sentinel. This ensures all bars are processed. The algorithm handles edge cases naturally, such as strictly increasing or decreasing sequences, without extra conditional logic. This approach demonstrates my ability to transform inefficient logic into linear time solutions, a skill essential for optimizing route calculation and surge pricing engines.

Common Mistakes to Avoid

  • Failing to append a zero-height bar at the end, leaving elements unprocessed in the stack
  • Calculating width incorrectly by forgetting to account for the index of the previous element in the stack
  • Implementing a nested loop solution that results in O(N^2) time complexity
  • Not handling duplicate heights correctly, leading to incorrect area calculations

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 145 Algorithms questionsBrowse all 57 Uber questions