Data Structures & AlgorithmsModerateLeetCode-style · TCS NQT
For daily temperatures [73, 74, 75, 71, 69, 72, 76, 73], the wait for day i is the number of days until a strictly warmer day (0 if none). What is the sum of all waits?
- A. 10
- B. 13
- C. 4
- D. 6
Correct answer: A. 10
Monotonic stack of indices with decreasing temperatures: when a warmer day arrives, pop and assign each popped day the distance. Checking only the next day misses waits longer than one day.
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
- Four processes need CPU bursts [5, 3, 8, 2] (all arrive at time 0). A round-robin scheduler gives each process up to a quantum of 2 units…
- Among all non-empty contiguous subarrays of 2 -1 4 -3 5 1, what is the largest sum that does NOT exceed 6?
- In how many ways can 1 2 2 0 3 2 1 2 2 be cut into exactly THREE non-empty contiguous parts whose sums are all equal? (A cut is a pair of…
- What is the maximum AVERAGE over all contiguous subarrays of 1 12 -5 -6 50 3 that have length at least 3? Give the answer as a fraction in…
- Choose two NON-OVERLAPPING contiguous windows of length exactly 2 from 4 -2 7 1 -5 6 3 -1. What is the largest possible total sum of their…
- How many contiguous subarrays of 3 1 2 4 0 contain EXACTLY 2 inversions (pairs i < j inside the subarray with a[i] > a[j])?