Data Structures & AlgorithmsVery hardProduct company OA
How many contiguous subarrays of [1, 3, 2, 4, 6, 5] satisfy max − min ≤ 2? (Two monotonic deques shrink the window from the left.)
- A. 21
- B. 8
- C. 14
- D. 15
Correct answer: C. 14
Valid windows ending at each right end: 1, 2, 3, 3, 2, 3, total 14. Because a valid window stays valid when shrunk, the two-pointer window adds (r − l + 1) per step. 8 uses spread < 2 instead of ≤ 2, 15 uses spread ≤ 3 and 21 counts every subarray.
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
- What is the smallest positive integer missing from [3, 4, −1, 1, 7, 2, 2, 9]? (In-place cyclic placement: put each value v with 1 ≤ v ≤ n…
- Which problem CANNOT be solved by a plain variable-size sliding window that shrinks from the left?
- The Boyer–Moore majority vote (candidate/counter, no verification pass) runs on [3, 5, 5, 7, 7, 7, 7, 7, 3]. What candidate and final…
- KMP failure function (lps[i] = length of the longest proper prefix of p[0..i] that is also a suffix). What is printed? python def lps(p)…
- The array [1, 2, 3, 4, 5, 6, 7, 8] is rotated RIGHT by 3 positions and then LEFT by 2 positions. What is the final array?
- Kadane's algorithm runs on [4, 0, 8, −3, −2, −2, −3, 2, −6]. What is the maximum sum of a non-empty contiguous subarray?