Data Structures & AlgorithmsHardFlipkart/Uber OA · placement
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, 30, 39, 31, 37, 35, 33, 35]
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. [0, 1, 0, 1, 0, 1, 0, 0] 3
- B. [0, 1, 0, 1, 0, 0, 1, 0] 3
- C. [2, 1, 0, 1, 0, 2, 1, 0] 7
- D. [0, 1, 0, 1, 0, 0, 0, 1] 3
Correct answer: B. [0, 1, 0, 1, 0, 0, 1, 0] 3
Indices wait on the stack until a strictly higher temperature arrives; equal temperatures do not count as warmer. Output: [0, 1, 0, 1, 0, 0, 1, 0] 3.
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
- Delete every alternate node (2nd, 4th, …) of a linked list, then reverse what remains. What is printed? python def delalt(h): p = h while…
- How many contiguous subarrays of [5, 3, 4, 5, 1, 1, 6, 5] have a sum between 6 and 10 inclusive? (All values positive: count atMost(10) −…
- Keys 80, 42, 55, 92, 96, 24, 95, 69 are inserted in this order into an empty hash table of size 11 with h(k) = k mod 11 and linear…
- Two pointers start at both ends of the sorted array [2, 5, 10, 11, 12, 14, 16, 18, 21, 29]. How many pairs (i < j) have a[i] + a[j] ≤ 28?…
- Selection sort (ascending; in pass i find the minimum of the unsorted part and swap it into position i only if it is not already there) is…
- Rotate a linked list to the RIGHT by k places (k may exceed the length). What is printed? python def rotate(h, k): n, t = 1, h while t.nx…