Data Structures & AlgorithmsVery hardCodeforces Div2 C/D · Amazon SDE OA

Histogram bars have heights h = [2, 1, 5, 6, 2, 3]. What is the area of the largest rectangle that fits entirely inside the histogram (bars have width 1)? Run the brute-force code over all bar heights and extents.

Histogram bars have heights h = [2, 1, 5, 6, 2, 3]. What is the area of the largest rectangle that fits entirely inside the histogram (bars have width 1)? Run the brute-force code over all bar heights and extents.
h=[2,1,5,6,2,3]
n=len(h)
print(max(min(h[i:j+1])*(j-i+1) for i in range(n) for j in range(i,n)))
What is printed?
  1. A. 6
  2. B. 10
  3. C. 12
  4. D. 20

Correct answer: B. 10

A rectangle is a contiguous run of bars times its minimum height, so brute force over runs gives the answer. Twice the maximum bar is not a rectangle here, and using the suffix width ignores the bars that are shorter than the current one.

More Coding Interview Problems questions

Exams that ask this topic