Data Structures & AlgorithmsHardProduct Company SDE · stack round
Problem: What is the largest rectangle area that can be formed in a histogram of heights [2, 1, 5, 6, 2, 3]? Use the stack code.
Problem: What is the largest rectangle area that can be formed in a histogram of heights [2, 1, 5, 6, 2, 3]? Use the stack code.
h = [2, 1, 5, 6, 2, 3] + [0]
st = []
best = 0
for i, x in enumerate(h):
start = i
while st and st[-1][1] >= x:
j, y = st.pop()
best = max(best, y * (i - j))
start = j
st.append((start, x))
print(best)What is printed?- A. 8
- B. 10
- C. 12
- D. 6
Correct answer: B. 10
The rectangle of height 5 and width 2 gives 10, and height 6 alone gives 6. The stack keeps increasing heights and pops when a shorter bar closes them.
More Coding Interview Problems questions
- Problem: What is the minimum number of operations to sort the array [4, 3, 2, 1] using only swaps of adjacent elements? Run the code…
- Problem: Consider a graph where you must find the minimum 'maximum edge' on a path from s to t (a bottleneck path). Which algorithm…
- Problem: For a segment tree with n leaves supporting range-sum queries and point updates, what is the total space needed in terms of n?
- Problem: Find the number of inversions in [5, 4, 3, 2, 1] that a merge-sort based counter would report. Run the code. python def count(a)…
- Problem: Which claim about the Knuth-Morris-Pratt algorithm is correct?
- Problem: A player picks a number from 1 to 10 and the other player must guess it by yes/no questions. The minimum worst-case number of…