Data Structures & AlgorithmsHard
For each contiguous window of length 3 in [1, 2, 1, 3, 2, 1], count the number of distinct values inside it, then add these counts over all windows. What is the total?
- A. 22
- B. 11
- C. 4
- D. 12
Correct answer: B. 11
For each window of length 3 the number of distinct values is the size of its set, and the total is the sum of these per-window counts. Adding the window sums, counting windows, or summing the maxima each measure something else.
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
- The numbers 1, 2, 3, 4, 5 are pushed onto an initially empty stack in this order, and a pop may be performed whenever the stack is…
- What is the minimum number of positive integers (each value at most 10, repetitions allowed in the array) needed so that EVERY integer…
- Given a sorted array, why is counting triplets with sum < T O(n²) rather than O(n³)?
- Floyd's cycle detection finds the slow and fast pointers meeting inside the loop. How is the START of the loop then found?
- In the greedy patching algorithm, the reachable range is [1..R] and the next sorted value x exceeds R+1. Why is R+1 the right patch?
- A monotonic deque for sliding-window maximum stores array INDICES instead of values. Why?