BackhardSortingAppleMicrosoft

Maximized Stream Minimum Solution

Problem Statement

You are given an array nums of length N representing a sequence of data points in a streaming system. The goal is to determine the maximum possible value of the minimum element in any contiguous subarray of length K.

Specifically, for every contiguous subarray of length K within nums, identify its minimum value. Among all these minimums, return the largest one. This metric is critical for assessing the worst-case performance guarantee over sliding windows in high-throughput pipelines.

Input: An integer array nums and an integer K. Output: An integer representing the maximum of the minimums of all contiguous subarrays of length K.

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

Explanation: The contiguous subarrays of length 3 are: [3,1,4] (min=1), [1,4,1] (min=1), [4,1,5] (min=1). The minimums are [1, 1, 1]. The maximum of these values is 1.

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

Explanation: The contiguous subarrays of length 2 are: [10,20] (min=10), [20,30] (min=20), [30,40] (min=30), [40,50] (min=40). The minimums are [10, 20, 30, 40]. The maximum of these values is 40.

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

Explanation: There is only one contiguous subarray of length 4: [5,5,5,5]. Its minimum is 5. The maximum of the set {5} is 5.

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

Explanation: The contiguous subarrays of length 3 are: [7,2,8] (min=2), [2,8,1] (min=1), [8,1,9] (min=1). The minimums are [2, 1, 1]. The maximum of these values is 2.

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= K <= nums.length
  • -10^9 <= nums[i] <= 10^9
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

Maximized Stream Minimum — Problem Statement & Solution Guide

SortingHardCustom Comparator
TimeO(N)
|
SpaceO(K)

Problem Description

You are given an array nums of length N representing a sequence of data points in a streaming system. The goal is to determine the maximum possible value of the minimum element in any contiguous subarray of length K.

Specifically, for every contiguous subarray of length K within nums, identify its minimum value. Among all these minimums, return the largest one. This metric is critical for assessing the worst-case performance guarantee over sliding windows in high-throughput pipelines.

Input: An integer array nums and an integer K.

Output: An integer representing the maximum of the minimums of all contiguous subarrays of length K.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Maximized Stream Minimum"

hard

WHY DOES IT MATTER?

Sliding‑window minima/maxima appear in performance monitoring, stock analysis, and real‑time anomaly detection where you need the worst‑case metric over a recent interval. Mastering the monotonic deque pattern lets you turn an otherwise quadratic scan into linear time, a critical skill for high‑throughput systems.

OPTIMIZATION CHALLENGE

The key insight is that only elements that could become the minimum of a future window need to be kept. By discarding any element larger than a newly arrived one from the deque’s tail, we guarantee the deque remains monotonic and each element is pushed and popped at most once, collapsing O(N·K) to O(N).

REAL-WORLD CONNECTION

Imagine a network router that keeps track of the smallest bandwidth observed over the last K packets to trigger throttling. The router cannot recompute the minimum from scratch for each packet; instead it maintains a deque of candidate minima, discarding stale or dominated measurements, mirroring the algorithmic solution.

When coding, treat the deque as a black‑box that always gives you the current window’s minimum at its front. Focus on correctly handling the out‑of‑range index removal before you push the new element; this order prevents subtle off‑by‑one bugs.

COMPLEXITY AT A GLANCE

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

Core Theory — Why This Approach?

The problem asks for the maximum among all sliding‑window minima of size K. A naïve solution would recompute the minimum for each window by scanning K elements, leading to O(N·K) time, which is prohibitive when N and K approach 10^5 or higher. The optimal paradigm leverages a monotonic deque (also called a sliding‑window minimum queue) that maintains candidates for the minimum in decreasing order. As the window slides, elements that fall out of the window are popped from the front, and any new element that is larger than the tail is simply appended, while smaller elements purge larger ones from the tail to preserve monotonicity. This yields the current window’s minimum in O(1) and updates the structure in amortized O(1) per element, resulting in overall O(N) time. After each window shift we record the deque’s front (the current minimum) and keep the global maximum of those minima, which is the answer.

Interview Questions on This Problem

Q1How would you find the maximum of minimums for all subarrays of length K in O(N) time?

Use a monotonic decreasing deque to maintain potential minima for the current window. For each index i, remove indices out of the window from the front, pop larger values from the back before pushing i, then the front holds the window’s minimum. Track the largest of these minima as you slide.

Q2Can you adapt the solution to also return the starting index of the window that yields the maximized minimum?

Yes. Alongside tracking the maximum minimum value, store the index of the window’s start when a new larger minimum is observed. The start index is i‑K+1 at that moment.

Q3What modifications are needed if the window size K can vary per query, and you must answer Q queries efficiently?

Preprocess the array using a Sparse Table or segment tree to answer range minimum queries in O(1) or O(log N). For each query with its own K, slide a virtual window by querying the minimum of each K‑length segment and keep the maximum; this runs in O(N) per query, but with offline techniques (e.g., Mo’s algorithm) you can achieve O((N+Q)√N) or better depending on constraints.

Examples

Example 1

Input

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

Output

1

Explanation: The contiguous subarrays of length 3 are: [3,1,4] (min=1), [1,4,1] (min=1), [4,1,5] (min=1). The minimums are [1, 1, 1]. The maximum of these values is 1.

Example 2

Input

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

Output

40

Explanation: The contiguous subarrays of length 2 are: [10,20] (min=10), [20,30] (min=20), [30,40] (min=30), [40,50] (min=40). The minimums are [10, 20, 30, 40]. The maximum of these values is 40.

Example 3

Input

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

Output

5

Explanation: There is only one contiguous subarray of length 4: [5,5,5,5]. Its minimum is 5. The maximum of the set {5} is 5.

Example 4

Input

nums = [7, 2, 8, 1, 9], K = 3

Output

2

Explanation: The contiguous subarrays of length 3 are: [7,2,8] (min=2), [2,8,1] (min=1), [8,1,9] (min=1). The minimums are [2, 1, 1]. The maximum of these values is 2.

Constraints

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

Optimal Approach & Strategy

Maintain a decreasing deque that stores indices of potential minima; update it as the window slides to get each window’s minimum in O(1) amortized, achieving O(N) total time.

Brute Force Approach

For each of the N‑K+1 windows, scan K elements to find its minimum, then keep the maximum of those minima. This is O(N·K) time.

Verified Code Solutions

JavaScript Solution
Time: O(N)
function solution(nums) {
   let maxSum = nums[0];
   let currentSum = nums[0];
   for (let i = 1; i < nums.length; i++) {
       currentSum = Math.max(nums[i], currentSum + nums[i]);
       maxSum = Math.max(maxSum, currentSum);
   }
   return maxSum;
}

Asked in Top Tech Interviews

AppleMicrosoft

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.