BackhardStackAppleGoldman Sachs

Centroid Tree Metric Engine 4 Solution

Problem Statement

You are tasked with optimizing a data stream processing pipeline that operates on a sequence of $N$ integer values. The system employs a sliding window mechanism of fixed size $K$ to analyze local trends. For each position $i$ in the input array (where $0 \le i < N$), if a valid window of size $K$ can be formed ending at index $i$ (i.e., $i \ge K-1$), the system must compute the maximum value within that specific window. If the window is not yet fully formed, the output for that position is defined as $-1$. Your objective is to generate an array of length $N$ where each element corresponds to the maximum value of the sliding window ending at that index, or $-1$ if the window is incomplete. This problem requires an efficient solution that processes the stream in linear time, leveraging the properties of monotonic queues to maintain the horizon of relevant candidates.

Example 1
Input
nums = [1, 3, -1, -3, 5, 3, 6, 7], K = 3
Output
[-1, -1, 3, 3, 5, 5, 6, 7]

Explanation: 1. Index 0: Window size < 3, output -1. 2. Index 1: Window size < 3, output -1. 3. Index 2: Window [1, 3, -1]. Max is 3. Output 3. 4. Index 3: Window [3, -1, -3]. Max is 3. Output 3. 5. Index 4: Window [-1, -3, 5]. Max is 5. Output 5. 6. Index 5: Window [-3, 5, 3]. Max is 5. Output 5. 7. Index 6: Window [5, 3, 6]. Max is 6. Output 6. 8. Index 7: Window [3, 6, 7]. Max is 7. Output 7.

Example 2
Input
nums = [10, 20, 30, 40, 50], K = 2
Output
[-1, 20, 30, 40, 50]

Explanation: 1. Index 0: Window size < 2, output -1. 2. Index 1: Window [10, 20]. Max is 20. Output 20. 3. Index 2: Window [20, 30]. Max is 30. Output 30. 4. Index 3: Window [30, 40]. Max is 40. Output 40. 5. Index 4: Window [40, 50]. Max is 50. Output 50.

Example 3
Input
nums = [5, 5, 5, 5], K = 4
Output
[-1, -1, -1, 5]

Explanation: 1. Index 0: Window size < 4, output -1. 2. Index 1: Window size < 4, output -1. 3. Index 2: Window size < 4, output -1. 4. Index 3: Window [5, 5, 5, 5]. Max is 5. Output 5.

Example 4
Input
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], K = 5
Output
[-1, -1, -1, -1, 5, 6, 7, 8, 9, 10]

Explanation: 1. Indices 0-3: Window size < 5, output -1. 2. Index 4: Window [1, 2, 3, 4, 5]. Max is 5. Output 5. 3. Index 5: Window [2, 3, 4, 5, 6]. Max is 6. Output 6. 4. Index 6: Window [3, 4, 5, 6, 7]. Max is 7. Output 7. 5. Index 7: Window [4, 5, 6, 7, 8]. Max is 8. Output 8. 6. Index 8: Window [5, 6, 7, 8, 9]. Max is 9. Output 9. 7. Index 9: Window [6, 7, 8, 9, 10]. Max is 10. Output 10.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • 1 <= K <= nums.length
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Centroid Tree Metric Engine 4 — Problem Statement & Solution Guide

StackHardMonotonic Queue Sliding Horizon
TimeO(N)
|
SpaceO(K)

Problem Description

You are tasked with optimizing a data stream processing pipeline that operates on a sequence of $N$ integer values. The system employs a sliding window mechanism of fixed size $K$ to analyze local trends. For each position $i$ in the input array (where $0 \le i < N$), if a valid window of size $K$ can be formed ending at index $i$ (i.e., $i \ge K-1$), the system must compute the maximum value within that specific window. If the window is not yet fully formed, the output for that position is defined as $-1$. Your objective is to generate an array of length $N$ where each element corresponds to the maximum value of the sliding window ending at that index, or $-1$ if the window is incomplete. This problem requires an efficient solution that processes the stream in linear time, leveraging the properties of monotonic queues to maintain the horizon of relevant candidates.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Centroid Tree Metric Engine 4"

hard

WHY DOES IT MATTER?

This pattern is essential for optimizing sliding window problems where you need to track an aggregate value (max, min, sum, etc.) efficiently. It demonstrates the power of amortized analysis and the use of specialized data structures to reduce time complexity from quadratic to linear.

OPTIMIZATION CHALLENGE

The key insight is that elements smaller than the new element are 'dominated' and can be discarded from the deque. This pruning step is what reduces the average case to O(1) per element, as each element is added and removed from the deque at most once.

REAL-WORLD CONNECTION

This is analogous to real-time stock price monitoring systems where you need to track the highest price in the last K minutes. The Monotonic Deque allows the system to update the maximum in O(1) time per new price tick, ensuring low-latency responses even with high-frequency data.

In interviews, clearly articulate the invariant of the deque: 'The deque stores indices of elements in decreasing order of their values.' This invariant is the core of the solution and helps the interviewer follow your logic.

COMPLEXITY AT A GLANCE

⏱ Time:O(N)
💾 Space:O(K)

Core Theory — Why This Approach?

The problem of finding the maximum in a sliding window of fixed size K over an array of size N is a classic application of the Monotonic Deque (Double-Ended Queue) data structure. A naive approach that scans the window for every position results in O(N*K) time complexity, which is infeasible for large N and K. The optimal paradigm leverages the property that we only care about the maximum element. By maintaining a deque of indices where the corresponding values are in decreasing order, we ensure that the front of the deque always holds the index of the current maximum. When a new element arrives, we remove all elements from the back of the deque that are smaller than the new element, as they can never be the maximum for any future window that includes the new element. This amortized O(1) operation per element leads to an overall O(N) time complexity.

Interview Questions on This Problem

Q1How would you adapt this solution to find the minimum in the sliding window instead of the maximum?

You would maintain the deque in increasing order instead of decreasing. When adding a new element, you remove elements from the back that are greater than the new element. The front of the deque will then always hold the index of the current minimum.

Q2What is the space complexity of the Monotonic Deque approach, and why is it bounded by K?

The space complexity is O(K) because the deque can hold at most K elements. In the worst case, if the array is strictly decreasing, the deque will hold all K elements of the current window. If the array is strictly increasing, the deque will hold only 1 element.

Q3How would you handle the case where K is greater than N?

If K is greater than N, no valid window of size K can be formed. The output should be an empty array or a specific error indicator, depending on the problem constraints. In code, you should check if K > N and return an empty result immediately.

Examples

Example 1

Input

nums = [1, 3, -1, -3, 5, 3, 6, 7], K = 3

Output

[-1, -1, 3, 3, 5, 5, 6, 7]

Explanation: 1. Index 0: Window size < 3, output -1. 2. Index 1: Window size < 3, output -1. 3. Index 2: Window [1, 3, -1]. Max is 3. Output 3. 4. Index 3: Window [3, -1, -3]. Max is 3. Output 3. 5. Index 4: Window [-1, -3, 5]. Max is 5. Output 5. 6. Index 5: Window [-3, 5, 3]. Max is 5. Output 5. 7. Index 6: Window [5, 3, 6]. Max is 6. Output 6. 8. Index 7: Window [3, 6, 7]. Max is 7. Output 7.

Example 2

Input

nums = [10, 20, 30, 40, 50], K = 2

Output

[-1, 20, 30, 40, 50]

Explanation: 1. Index 0: Window size < 2, output -1. 2. Index 1: Window [10, 20]. Max is 20. Output 20. 3. Index 2: Window [20, 30]. Max is 30. Output 30. 4. Index 3: Window [30, 40]. Max is 40. Output 40. 5. Index 4: Window [40, 50]. Max is 50. Output 50.

Example 3

Input

nums = [5, 5, 5, 5], K = 4

Output

[-1, -1, -1, 5]

Explanation: 1. Index 0: Window size < 4, output -1. 2. Index 1: Window size < 4, output -1. 3. Index 2: Window size < 4, output -1. 4. Index 3: Window [5, 5, 5, 5]. Max is 5. Output 5.

Example 4

Input

nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], K = 5

Output

[-1, -1, -1, -1, 5, 6, 7, 8, 9, 10]

Explanation: 1. Indices 0-3: Window size < 5, output -1. 2. Index 4: Window [1, 2, 3, 4, 5]. Max is 5. Output 5. 3. Index 5: Window [2, 3, 4, 5, 6]. Max is 6. Output 6. 4. Index 6: Window [3, 4, 5, 6, 7]. Max is 7. Output 7. 5. Index 7: Window [4, 5, 6, 7, 8]. Max is 8. Output 8. 6. Index 8: Window [5, 6, 7, 8, 9]. Max is 9. Output 9. 7. Index 9: Window [6, 7, 8, 9, 10]. Max is 10. Output 10.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • 1 <= K <= nums.length

Optimal Approach & Strategy

Use a Monotonic Deque to maintain indices of elements in decreasing order. For each new element, remove smaller elements from the back and out-of-window elements from the front. The front of the deque gives the current maximum in O(1) amortized time.

Brute Force Approach

For each position i, scan the window from i-K+1 to i to find the maximum value. This results in O(N*K) time complexity, which is too slow for large inputs.

Verified Code Solutions

JavaScript Solution
Time: O(N)
function solution(nums) {
   let queue = [];
   let maxSum = -Infinity;
   for (let i = 0; i < nums.length; i++) {
       while (queue.length > 0 && nums[queue[queue.length - 1]] < nums[i]) {
           queue.pop();
       }
       queue.push(i);
       maxSum = Math.max(maxSum, nums[queue[0]] + nums[queue[queue.length - 1]]);
   }
   return maxSum;
}

Asked in Top Tech Interviews

AppleGoldman Sachs

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.