ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
def hist(h):
    st = []
    best = 0
    h = h + [0]
    for i, x in enumerate(h):
        start = i
        while st and st[-1][1] >= x:
            idx, height = st.pop()
            best = max(best, height * (i - idx))
            start = idx
        st.append((start, x))
    return best
print(hist([2, 1, 5, 6, 2, 3]), hist([2, 4]))
  1. A. 10 4
  2. B. 8 4
  3. C. 10 2
  4. D. 12 4

Correct answer: A. 10 4

The best rectangle in [2, 1, 5, 6, 2, 3] spans the bars 5 and 6, giving 5 x 2 = 10. In [2, 4] the bar of height 4 alone gives 4.

More Python Programming questions

Exams that ask this topic