Sliding Window Maximum

Algorithms
Hard
Google
58K views

Given an array `nums` and a sliding window size $k$, find the maximum number in each window. Use a Deque (Double-Ended Queue) for an $O(n)$ solution.

Why Interviewers Ask This

Google engineers ask this to evaluate your ability to optimize time complexity beyond naive solutions. They specifically test if you can leverage a Deque to maintain state efficiently, demonstrating mastery of data structures and sliding window patterns essential for high-throughput systems.

How to Answer This Question

1. Clarify constraints: Confirm input size and whether k is valid. 2. Propose the brute force O(n*k) solution first to establish a baseline, then immediately pivot to the optimal approach. 3. Explain the Deque strategy: Describe how indices are stored so the front always holds the maximum index, and elements smaller than the current number are removed from the back. 4. Walk through the logic: Detail how you slide the window, remove expired indices (those outside the current k range), and add the new element while maintaining order. 5. Analyze complexity: Explicitly state why each element is added and removed at most once, proving O(n) time and O(k) space. 6. Implement carefully: Write clean code handling edge cases like empty arrays or k=1.

Key Points to Cover

  • Demonstrating knowledge of the Deque data structure for monotonic queue optimization
  • Explaining the O(n) time complexity proof based on single-pass operations
  • Handling edge cases like invalid k values or empty input arrays gracefully
  • Clearly articulating the logic for removing expired indices from the front
  • Maintaining the descending order invariant within the Deque during iteration

Sample Answer

To solve the Sliding Window Maximum problem efficiently, I would start by acknowledging that a brute force approach checking every window takes O(n*k) time, which is inefficient for large datasets Google often handles. Instead, I propose using a Deque to achieve O(n) time complexity. The core idea is to store indices in the Deque such that the corresponding values are in descending order. As we iterate through the array, before adding the current index, we remove all indices from the back of the Deque whose values are less than or equal to the current number, as they can never be the maximum again. Next, we check the front of the Deque; if the index there is out of the current window (i.e., less than i-k+1), we remove it. Finally, the front of the Deque always represents the index of the maximum value for the current window. For example, with nums=[1,3,-1,-3,5,3,6] and k=3, when we reach 5, we pop -1 and -3 because 5 is larger, ensuring 5 is at the front. This ensures each element is pushed and popped at most once. This approach mirrors Google's focus on scalable algorithms where constant factors matter significantly in production environments.

Common Mistakes to Avoid

  • Using a priority queue which results in O(n log k) instead of the required O(n)
  • Forgetting to remove indices that have slid out of the window from the front
  • Storing actual values instead of indices, making it impossible to check window boundaries
  • Implementing the solution without explaining why the monotonic property holds

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 145 Algorithms questionsBrowse all 87 Google questions