Data Structures & AlgorithmsHardIBM · Deloitte pattern
What is the sum of the minimum element over every contiguous subarray of [3, 6, 5, 3, 8, 4, 9, 1]?
- A. 131
- B. 113
- C. 263
- D. 114
Correct answer: D. 114
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 114. 263 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
- Two sorted linked lists [7 → 13 → 23 → 40] and [5 → 6 → 9 → 18 → 25] are merged with the standard O(n+m) two-pointer merge (always take…
- The Dutch National Flag algorithm (single pass, three pointers low/mid/high) partitions [2, 0, 2, 2, 1, 0, 1, 1] (values 0, 1, 2 only) in…
- Array [85, 11, 19, 30, 40, 43, 70] is a sorted array rotated at pivot 6. Using the standard O(log n) rotated-array binary search (decide…
- A min-stack (push/pop O(1), getMin O(1) via an auxiliary stack tracking the running minimum) runs this sequence in order: push(-2)…
- Array [1, 2, 3, 4, 5, 6, 7, 8, 9] is left-rotated by d = 6 positions using the reversal algorithm (reverse first d, reverse rest, reverse…
- An LRU cache of capacity 3 runs this sequence of operations in order: put(4, 40), put(2, 20), put(5, 50), put(1, 10), put(1, 10), put(2…