Data Structures & AlgorithmsHardICPC regional · Amazon SDE OA
Given an array of 10^6 values and window size k, you need the maximum over all windows of the window minimum. What is the most suitable approach?
- A. A segment tree built per window, giving O(n k log n)
- B. Sorting each window separately, giving O(n k log k)
- C. A monotonic deque for sliding-window minima, giving O(n) total time
- D. Brute-force scanning with a priority queue of all windows, giving O(n^2)
Correct answer: C. A monotonic deque for sliding-window minima, giving O(n) total time
The deque keeps candidates in increasing order and expires indices outside the window, so each element enters and leaves once. The other choices do much more work per window.
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
- How many contiguous subarrays of [1, 2, 1, 3, 2, 2, 1] contain at most two distinct values?
- You may increase any element of [2, 5, 3, 9] by 1 per operation, and you want all elements equal. What is the minimum number of operations?
- In the binary row [1, 1, 0, 1, 1, 1] treated as a histogram of height 1, what is the largest rectangle area?
- What is the length of the shortest contiguous subarray of [5, 1, 4, 3, 2, 6, 7] whose sum is at least 15?
- In [6, 3, 6, 9, 3, 9, 9, 5, 5] exactly one value appears an odd number of times and every other value appears an even number of times…
- How many index pairs i < j in [12, 21, 30, 43, 66, 111, 3] have equal decimal digit sums?