Data Structures & AlgorithmsModerateTCS NQT · Product Company SDE
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 maxima?
- A. 29
- B. 22
- C. 38
- D. 15
Correct answer: A. 29
The six windows have maxima 3, 3, 5, 5, 6, 7, summing to 29 (a deque keeps it O(n)). Looping over only n − 3 windows misses the last one (22), summing window totals instead of maxima gives 38, and using non-overlapping blocks gives 3 + 5 + 7 = 15.
More Coding Interview Problems questions
- 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.
- Problem: On a line of 12 cells (1–12), a robot starts at cell 1. Each second it moves to the cell i+3 if that cell is still ≤ 12 and not…