Data Structures & AlgorithmsHardCodeforces Div2 B · DP warm-up
Problem: A sliding window maximum over an array of size n with window size k is computed with a monotonic deque. What is its time complexity, and why not just recompute the max for every window naively?
- A. O(n log k) with the deque, O(n) naively
- B. O(n) with the deque (each index pushed/popped once); the naive approach recomputing the max per window is O(nk)
- C. O(nk) for both approaches
- D. O(n) for both, since the naive approach is already optimal
Correct answer: B. O(n) with the deque (each index pushed/popped once); the naive approach recomputing the max per window is O(nk)
The deque keeps indices in decreasing value order and each index is added and removed at most once, giving O(n) total; naively scanning all k elements for every one of the (n-k+1) windows costs O(nk).
More Coding Interview Problems questions
- Problem: Two Sum on an unsorted array can be solved in O(n) using a hash map, or in O(n log n) using sort-and-two-pointers. Why might the…
- Problem: Three Sum (find all triplets summing to zero) is commonly solved by sorting the array and then applying two pointers for each…
- Problem: Which statement about Dijkstra's algorithm and negative edge weights is correct?
- Problem: Which best describes the relationship between DFS-based cycle detection in a directed graph and the 'white-gray-black' (or…
- Problem: A bipartite graph check is commonly done by attempting a 2-coloring via BFS/DFS. What does finding an edge between two…
- Problem: What is the time complexity of finding the lowest common ancestor (LCA) of two nodes in a binary tree with n nodes, using a…