BackmediumRecursionAccentureMicrosoft

Resilient Stream Minimum Solution

Problem Statement

Consider a data stream represented as an array of integers where each element arrives sequentially. The 'Resilient Stream Minimum' is defined as the smallest value observed in the entire sequence. Your task is to implement a recursive function that processes the array by dividing it into sub-problems, determining the minimum value of each partition, and combining these results to yield the global minimum.

The recursive approach must strictly follow a divide-and-conquer strategy: split the array into two halves, recursively compute the minimum for each half, and return the smaller of the two results. The base case occurs when the sub-array contains only one element, which is returned as the minimum of that segment.

Input: An array of integers nums. Output: A single integer representing the minimum value in the array.

Example 1
Input
nums = [3, 1, 4, 1, 5, 9, 2, 6]
Output
1

Explanation: The array is split into [3, 1, 4, 1] and [5, 9, 2, 6]. The left half splits into [3, 1] and [4, 1]. [3, 1] yields min(3, 1) = 1. [4, 1] yields min(4, 1) = 1. The left half minimum is min(1, 1) = 1. The right half splits into [5, 9] and [2, 6]. [5, 9] yields min(5, 9) = 5. [2, 6] yields min(2, 6) = 2. The right half minimum is min(5, 2) = 2. The global minimum is min(1, 2) = 1.

Example 2
Input
nums = [10, -5, 0, 7, -12, 3]
Output
-12

Explanation: Split into [10, -5, 0] and [7, -12, 3]. Left half: [10, -5] -> min(10, -5) = -5; [0] -> 0. Left min = min(-5, 0) = -5. Right half: [7, -12] -> min(7, -12) = -12; [3] -> 3. Right min = min(-12, 3) = -12. Global min = min(-5, -12) = -12.

Example 3
Input
nums = [42]
Output
42

Explanation: Base case: The array has length 1. The recursive function immediately returns the single element 42 as the minimum.

Example 4
Input
nums = [8, 8, 8, 8]
Output
8

Explanation: Split into [8, 8] and [8, 8]. Left half: min(8, 8) = 8. Right half: min(8, 8) = 8. Global min = min(8, 8) = 8.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • The recursion depth must not exceed the system stack limit (typically O(log n) for balanced splits).
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

Resilient Stream Minimum — Problem Statement & Solution Guide

RecursionMediumBacktracking Path
TimeO(n)
|
SpaceO(log n)

Problem Description

Consider a data stream represented as an array of integers where each element arrives sequentially. The 'Resilient Stream Minimum' is defined as the smallest value observed in the entire sequence. Your task is to implement a recursive function that processes the array by dividing it into sub-problems, determining the minimum value of each partition, and combining these results to yield the global minimum.

The recursive approach must strictly follow a divide-and-conquer strategy: split the array into two halves, recursively compute the minimum for each half, and return the smaller of the two results. The base case occurs when the sub-array contains only one element, which is returned as the minimum of that segment.

Input: An array of integers nums.

Output: A single integer representing the minimum value in the array.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Resilient Stream Minimum"

medium

WHY DOES IT MATTER?

Divide‑and‑conquer is a foundational pattern for breaking large problems into manageable sub‑problems, enabling logarithmic recursion depth and reducing risk of stack overflow while preserving linear work.

OPTIMIZATION CHALLENGE

The key insight is to halve the problem size at each recursive step, turning a potentially O(n) recursion depth into O(log n) and keeping auxiliary space proportional to the recursion depth rather than the input size.

REAL-WORLD CONNECTION

Think of a distributed monitoring system that aggregates the smallest latency metric from multiple data centers: each center reports its local minimum, and a central aggregator picks the smallest among them, mirroring the recursive merge step.

When coding, always pass the sub‑array boundaries (left, right) instead of creating new slices; this avoids O(n) extra memory and keeps the recursion truly in‑place.

COMPLEXITY AT A GLANCE

⏱ Time:O(n)
đź’ľ Space:O(log n)

Core Theory — Why This Approach?

The Resilient Stream Minimum problem is a classic example of divide‑and‑conquer applied to a linear data structure. By recursively splitting the array into two halves, solving the sub‑problem (finding the minimum in each half), and then merging the two results with a simple comparison, we achieve a logarithmic recursion depth while still touching every element exactly once. This approach leverages the optimal substructure property: the minimum of the whole array is the lesser of the minima of its partitions, and the overlapping sub‑problems are independent, allowing a clean recursive formulation.

A naïve linear scan works in O(n) time but does not illustrate recursion, which is often required in interview settings to assess a candidate’s ability to think recursively and manage stack frames. Moreover, a naïve recursive implementation that processes one element per call (e.g., recursing on n‑1 elements each time) incurs O(n) recursion depth, risking stack overflow for large inputs. The optimal divide‑and‑conquer paradigm reduces the depth to O(log n) by halving the problem size at each step, preserving linear overall time while keeping the call stack shallow and manageable.

Interview Questions on This Problem

Q1How would you modify the recursive minimum algorithm to also return the index of the minimum element?

Return a pair (value, index) from each recursive call; when merging, compare the values and propagate the corresponding index. This adds only O(1) extra work per merge.

Q2Can you adapt the solution to work on a read‑only stream where you cannot store the entire array in memory?

Use a tail‑recursive or iterative approach that maintains a single variable for the current minimum while processing each incoming element; recursion is replaced by a loop to respect the read‑only constraint.

Q3What is the impact on time and space complexity if the recursion is implemented without tail‑call optimization in a language like Java?

Time remains O(n) because each element is visited once, but space becomes O(log n) for the call stack due to the divide‑and‑conquer splits; without tail‑call optimization, this is still acceptable, whereas a linear recursion would degrade to O(n) stack space.

Examples

Example 1

Input

nums = [3, 1, 4, 1, 5, 9, 2, 6]

Output

1

Explanation: The array is split into [3, 1, 4, 1] and [5, 9, 2, 6]. The left half splits into [3, 1] and [4, 1]. [3, 1] yields min(3, 1) = 1. [4, 1] yields min(4, 1) = 1. The left half minimum is min(1, 1) = 1. The right half splits into [5, 9] and [2, 6]. [5, 9] yields min(5, 9) = 5. [2, 6] yields min(2, 6) = 2. The right half minimum is min(5, 2) = 2. The global minimum is min(1, 2) = 1.

Example 2

Input

nums = [10, -5, 0, 7, -12, 3]

Output

-12

Explanation: Split into [10, -5, 0] and [7, -12, 3]. Left half: [10, -5] -> min(10, -5) = -5; [0] -> 0. Left min = min(-5, 0) = -5. Right half: [7, -12] -> min(7, -12) = -12; [3] -> 3. Right min = min(-12, 3) = -12. Global min = min(-5, -12) = -12.

Example 3

Input

nums = [42]

Output

42

Explanation: Base case: The array has length 1. The recursive function immediately returns the single element 42 as the minimum.

Example 4

Input

nums = [8, 8, 8, 8]

Output

8

Explanation: Split into [8, 8] and [8, 8]. Left half: min(8, 8) = 8. Right half: min(8, 8) = 8. Global min = min(8, 8) = 8.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • The recursion depth must not exceed the system stack limit (typically O(log n) for balanced splits).

Optimal Approach & Strategy

Recursively split the array in half, compute the minimum of each half, and combine them with a single comparison, achieving O(n) time and O(log n) stack space.

Brute Force Approach

Iterate through the array once, keeping track of the smallest value seen so far.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function solution(nums) { return Math.min(...nums); }

Asked in Top Tech Interviews

AccentureMicrosoft

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.