BackmediumStackOracleAccenture

Balanced Stream Minimum Solution

Problem Statement

Given an array or sequence of length N representing numerical values or system metrics, compute the balanced stream minimum according to the target algorithm rules.

Example 1
Input
[3, 2, 11, 10]
Output
26

Explanation: Step-by-step: push 3 to the stack, sum = 3. Push 2 to the stack, sum = 3 + 2 = 5. Push 11 to the stack, sum = 5 + 11 = 16. Push 10 to the stack, sum = 16 + 10 = 26. The balanced stream minimum is 26.

Example 2
Input
[10, 10, 10, 10]
Output
40

Explanation: Step-by-step: push 10 to the stack, sum = 10. Push 10 to the stack, sum = 10 + 10 = 20. Push 10 to the stack, sum = 20 + 10 = 30. Push 10 to the stack, sum = 30 + 10 = 40. The balanced stream minimum is 40.

Constraints

  • 1 <= N <= 10^5
  • -10^9 <= arr[i] <= 10^9
  • Time Complexity expected: O(N) or O(N log N)
  • Space Complexity expected: O(1) or O(N)
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

Balanced Stream Minimum — Problem Statement & Solution Guide

StackMediumNext Greater Element
TimeO(N)
|
SpaceO(K)

Problem Description

Given an array or sequence of length N representing numerical values or system metrics, compute the balanced stream minimum according to the target algorithm rules.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Balanced Stream Minimum"

medium

WHY DOES IT MATTER?

The deque-based sliding window minimum is a canonical example of amortized analysis and monotonic data structures. Mastering this pattern equips engineers to solve a wide range of real-time analytics, such as computing rolling minima for CPU usage, latency, or financial time series, where performance and memory constraints are critical.

OPTIMIZATION CHALLENGE

The key insight is to discard elements that can never be the minimum again, reducing the number of comparisons from O(K) per window to O(1) amortized. This transforms a quadratic scan into a linear pass.

REAL-WORLD CONNECTION

In distributed monitoring systems, you often need to compute the minimum latency over the last N seconds for each service. A deque allows each metric to be processed in constant time, enabling sub-second updates across thousands of services without bottlenecks.

When explaining this to an interviewer, emphasize the amortized analysis: each element is pushed and popped at most once, so the overall complexity is linear. Also, mention that the deque can be implemented with a fixed-size array for extra speed in production.

COMPLEXITY AT A GLANCE

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

Core Theory — Why This Approach?

Balanced Stream Minimum is a classic sliding window problem that can be solved efficiently using a double-ended queue (deque) or a monotonic stack. The naive approach would recompute the minimum for each window by scanning all elements inside the window, leading to an O(N·K) time complexity where K is the window size; this becomes infeasible for large streams or real-time analytics. The optimal paradigm maintains a data structure that preserves the order of candidate minima while discarding elements that can never become the minimum again. By pushing new elements onto the deque and popping from the back any elements larger than the new one, we guarantee that the front of the deque always holds the current window’s minimum. This yields an O(N) time solution with O(K) auxiliary space.

The underlying theory hinges on the concept of *monotonicity*: a data structure that keeps its elements in a sorted order relative to the operation being performed. In the context of a sliding window minimum, the deque is monotonic increasing; each new element is inserted in a way that all larger elements to its left are removed because they will never be the minimum while the new element is present. This property ensures that each element is inserted and removed at most once, giving linear time.

Moreover, the algorithm is a specific instance of the *deque trick* used in many online and streaming problems (e.g., maximum subarray, longest increasing subsequence). Understanding this pattern allows engineers to generalize the solution to other window-based queries and to reason about amortized complexity, which is essential for high-performance systems that process millions of metrics per second.

Interview Questions on This Problem

Q1What are the time and space complexities of the optimal solution for the Balanced Stream Minimum problem, and how do they compare to the naive approach?

The optimal solution runs in O(N) time and uses O(K) auxiliary space, where K is the window size. The naive approach, which scans each window to find its minimum, runs in O(N·K) time and uses O(1) space. Thus, the optimal solution is linear in the input size and scales far better for large streams.

Q2Can you explain how the monotonic property of the deque ensures that each element is inserted and removed at most once?

When a new element arrives, all elements larger than it are removed from the back because they can never become the minimum while the new element is in the window. Since each element is removed only when a smaller element arrives, and each element is inserted once, the total number of insertions and deletions is bounded by 2N, leading to amortized O(1) per operation.

Examples

Example 1

Input

[3, 2, 11, 10]

Output

26

Explanation: Step-by-step: push 3 to the stack, sum = 3. Push 2 to the stack, sum = 3 + 2 = 5. Push 11 to the stack, sum = 5 + 11 = 16. Push 10 to the stack, sum = 16 + 10 = 26. The balanced stream minimum is 26.

Example 2

Input

[10, 10, 10, 10]

Output

40

Explanation: Step-by-step: push 10 to the stack, sum = 10. Push 10 to the stack, sum = 10 + 10 = 20. Push 10 to the stack, sum = 20 + 10 = 30. Push 10 to the stack, sum = 30 + 10 = 40. The balanced stream minimum is 40.

Constraints

  • 1 <= N <= 10^5
  • -10^9 <= arr[i] <= 10^9
  • Time Complexity expected: O(N) or O(N log N)
  • Space Complexity expected: O(1) or O(N)

Optimal Approach & Strategy

Use a deque to maintain a monotonic increasing sequence of candidate minima. Each element is inserted once and removed at most once, giving O(N) time and O(K) auxiliary space.

Brute Force Approach

The naive solution scans each window of size K and finds its minimum by iterating over all K elements. This results in O(N·K) time and O(1) space, which is impractical for large N or real-time streams.

Verified Code Solutions

JavaScript Solution
Time: O(N)
function solution(nums) {
   let stack = [];
   let sum = 0;
   for (let num of nums) {
       while (stack.length > 0 && stack[stack.length - 1] < num) {
           sum -= stack.pop();
       }
       stack.push(num);
       sum += num;
   }
   return sum;
}

Asked in Top Tech Interviews

OracleAccenture

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.