Data Structures & AlgorithmsHardTCS NQT Digital · Product Company SDE
Problem: For the array [4, 1, 5, 2, 6, 3, 7], compute, for each index i, the distance to the next strictly greater element to its right (0 if none exists, and the distance is index difference). What is the sum of these distances?
- A. 3
- B. 24
- C. 10
- D. 9
Correct answer: D. 9
A monotonic stack gives next-greater distances 2, 1, 2, 1, 2, 1, 0, summing to 9. Looking left for the previous greater element gives 3, adding the target indices instead of distances gives 24, and charging n − i for the last element instead of 0 gives 10.
More Coding Interview Problems questions
- Problem: The maximum of each window of length 3 in [1, 3, −1, −3, 5, 3, 6, 7] is recorded for all windows. What is the sum of these window…
- Problem: Ship packages in order (weights 4, 7, 2, 9, 5, 3, 8, 6, 1) through consecutive days, with each day's load at most a capacity C…
- Problem: Count the triples of indices i < j < k in [2, 5, 1, 7, 3, 4, 6] with a[i] + a[j] + a[k] ≤ 12.
- Problem: How many integers n with 1 ≤ n ≤ 500 satisfy n mod 3 = n mod 5 ?
- Problem: A matrix of size 4×4 is filled row-wise with 1 to 16 (row 0 holds 1–4, row 1 holds 5–8, and so on). It is rotated 90 degrees…
- Problem: In the string "racecarbanana", find the number of distinct palindromic substrings of length at least 3.