Data Structures & AlgorithmsHardTCS NQT · Infosys pattern
What is the sum of the minimum element over every contiguous subarray of [9, 4, 5, 3, 1, 9, 8, 1]?
- A. 265
- B. 90
- C. 103
- D. 89
Correct answer: B. 90
Using a monotonic stack, each a[i] contributes a[i] × (elements between it and the previous smaller) × (elements between it and the next smaller-or-equal). Summing this (or the direct brute force over all 36 subarrays) gives 90. 265 is the sum of the *maximum* of each subarray instead, a different quantity.
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
- QuickSort (last element as pivot, Lomuto-style partition) sorts [31, 34, 38, 59, 23, 16, 54, 42, 46]. Counting one comparison per element…
- Array [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] is left-rotated by d = 2 positions using the reversal algorithm (reverse first d, reverse rest…
- Two sorted linked lists [9 → 14 → 35 → 36 → 37] and [27 → 29 → 31 → 32] are merged with the standard O(n+m) two-pointer merge (always take…
- Kadane's algorithm runs on the array [-9, 3, 3, -2, 6, 4, -4, 6, 9] to find the maximum sum of a contiguous subarray. What sum does it…
- Elevation map [4, 6, 0, 2, 6, 6, 6, 2] (bar width 1). Using the two-pointer / prefix-max approach for trapping rain water, how much water…
- Using a sliding window (shrink from the left whenever more than k distinct characters are in the window), what is the length of the…