Data Structures & AlgorithmsHardProduct placement · Amazon/Microsoft
For each day, a monotonic stack finds how many days you must wait for a strictly warmer temperature (0 if never). What is printed?
For each day, a monotonic stack finds how many days you must wait for a strictly warmer temperature (0 if never). What is printed?
t = [39, 35, 33, 40, 34, 33, 38, 40]
res, st = [0] * len(t), []
for i, x in enumerate(t):
while st and t[st[-1]] < x:
j = st.pop(); res[j] = i - j
st.append(i)
print(res, sum(res))- A. [3, 2, 1, 0, 1, 2, 1, 0] 10
- B. [3, 2, 1, 4, 2, 1, 1, 0] 14
- C. [3, 2, 0, 1, 2, 1, 1, 0] 10
- D. [3, 2, 1, 0, 2, 1, 1, 0] 10
Correct answer: D. [3, 2, 1, 0, 2, 1, 1, 0] 10
Indices wait on the stack until a strictly higher temperature arrives; equal temperatures do not count as warmer. Output: [3, 2, 1, 0, 2, 1, 1, 0] 10.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Count palindromic substrings by expanding around all 2n − 1 centres, and also count DISTINCT palindromic substrings. What is printed?…
- An array holds n distinct elements. What is the minimum number of comparisons that ANY comparison-based algorithm needs, in the worst…
- Shell sort with gap sequence 4, 2, 1 (gapped insertion sort for each gap). What is the array after the gap-4 pass only? python a = [35, 8…
- Keys 107, 29, 82, 81, 18, 115, 85 are inserted in this order into an empty table of size 17 using quadratic probing: probe i goes to (k…
- Keys 80, 25, 99, 55, 30, 13, 57, 42 are inserted in this order into an empty table of size 13 with h(k) = k mod 13 and linear probing…
- A queue is implemented with two stacks (push to IN; pop from OUT, refilling OUT from IN only when OUT is empty). Which statement is correct?