BackmediumArraysUberAdobe

Optimal Stream Minimum Solution

Problem Statement

You are processing a continuous data stream represented as a linear array of integers. The objective is to determine the global minimum value present in the entire sequence. This metric is critical for establishing the lower bound of the dataset for subsequent normalization or outlier detection tasks.

Given an array nums of length N, identify the smallest integer contained within the collection. The solution must scan the entire array to ensure no smaller value is overlooked, as the data is not guaranteed to be sorted or partitioned.

If the input array is empty, the function must return Infinity to signify the absence of any valid data points. Otherwise, return the minimum integer value found. The algorithm should operate in linear time relative to the array size, ensuring efficiency for large-scale data streams.

Example 1
Input
nums = [42, 17, 93, 5, 88, 21]
Output
5

Explanation: Initialize the minimum value to the first element, 42. Compare with 17: 17 is smaller, update min to 17. Compare with 93: 93 is larger, min remains 17. Compare with 5: 5 is smaller, update min to 5. Compare with 88: 88 is larger, min remains 5. Compare with 21: 21 is larger, min remains 5. The final minimum is 5.

Example 2
Input
nums = [-10, -5, -20, -1, -15]
Output
-20

Explanation: Start with min = -10. Compare with -5: -5 > -10, min stays -10. Compare with -20: -20 < -10, update min to -20. Compare with -1: -1 > -20, min stays -20. Compare with -15: -15 > -20, min stays -20. The global minimum is -20.

Example 3
Input
nums = [7]
Output
7

Explanation: The array contains a single element. By definition, the minimum of a singleton set is the element itself. No comparisons are needed beyond initialization. Return 7.

Example 4
Input
nums = []
Output
Infinity

Explanation: The input array is empty. According to the problem specification, an empty stream has no defined minimum value. The function returns Infinity to represent the upper bound of the real number line, indicating no data was processed.

Constraints

  • 0 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • Time Complexity: O(N)
  • Space Complexity: O(1)
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

Optimal Stream Minimum — Problem Statement & Solution Guide

ArraysMediumSubarray Generation
TimeO(N)
|
SpaceO(1)

Problem Description

You are processing a continuous data stream represented as a linear array of integers. The objective is to determine the global minimum value present in the entire sequence. This metric is critical for establishing the lower bound of the dataset for subsequent normalization or outlier detection tasks.

Given an array nums of length N, identify the smallest integer contained within the collection. The solution must scan the entire array to ensure no smaller value is overlooked, as the data is not guaranteed to be sorted or partitioned.

If the input array is empty, the function must return Infinity to signify the absence of any valid data points. Otherwise, return the minimum integer value found. The algorithm should operate in linear time relative to the array size, ensuring efficiency for large-scale data streams.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Optimal Stream Minimum"

medium

WHY DOES IT MATTER?

This pattern is essential because it forms the basis for understanding linear scans, which are the most common operation in data processing. It teaches the importance of initializing variables correctly and handling edge cases like empty arrays or single-element arrays.

OPTIMIZATION CHALLENGE

The key insight is that you only need to keep track of the current minimum as you iterate through the array. This reduces the space complexity to O(1) and ensures that each element is processed exactly once, leading to O(N) time complexity.

REAL-WORLD CONNECTION

In distributed systems, finding the minimum value across multiple nodes is a common operation in consensus algorithms and data aggregation. For example, in a load balancer, finding the node with the least load (minimum) is critical for distributing traffic efficiently.

In interviews, always mention edge cases like empty arrays or arrays with negative numbers. Show that you understand the importance of initializing the minimum variable to the first element rather than a hardcoded value like 0 or Integer.MAX_VALUE, which can lead to bugs if the array contains only negative numbers.

COMPLEXITY AT A GLANCE

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

Core Theory — Why This Approach?

Finding the global minimum in a linear array is a foundational problem in algorithm design that serves as the basis for more complex data structures like segment trees and sparse tables. While the problem appears trivial, it introduces the concept of linear scan optimization, where we must visit every element exactly once to guarantee correctness. The naive approach of repeatedly scanning the array for each query is inefficient, leading to O(N^2) complexity for multiple queries, which is unacceptable in high-throughput stream processing environments.

Interview Questions on This Problem

Q1How would you modify this solution to handle a dynamic stream where elements are added one by one, and you need to retrieve the minimum in O(1) time?

Use a monotonic stack or a specialized data structure like a Min-Heap. For a stream, a Min-Heap allows O(log N) insertion and O(1) retrieval of the minimum. If deletions are also required, a balanced BST or a segment tree might be more appropriate, but for pure insertion and min-query, a heap is optimal.

Q2What is the time complexity of finding the minimum if the array is already sorted?

If the array is sorted in ascending order, the minimum is simply the first element, making the time complexity O(1). However, if the array is sorted in descending order, the minimum is the last element, still O(1). The key is recognizing the sorted property to avoid a full scan.

Q3How would you handle integer overflow when comparing elements in a language like C++ or Java?

When comparing integers, ensure that the comparison logic does not involve arithmetic operations that could overflow. For simple minimum finding, direct comparison (a < b) is safe. If the problem involved calculating differences or sums, you would need to use larger data types (e.g., long long) or careful conditional logic to prevent overflow.

Examples

Example 1

Input

nums = [42, 17, 93, 5, 88, 21]

Output

5

Explanation: Initialize the minimum value to the first element, 42. Compare with 17: 17 is smaller, update min to 17. Compare with 93: 93 is larger, min remains 17. Compare with 5: 5 is smaller, update min to 5. Compare with 88: 88 is larger, min remains 5. Compare with 21: 21 is larger, min remains 5. The final minimum is 5.

Example 2

Input

nums = [-10, -5, -20, -1, -15]

Output

-20

Explanation: Start with min = -10. Compare with -5: -5 > -10, min stays -10. Compare with -20: -20 < -10, update min to -20. Compare with -1: -1 > -20, min stays -20. Compare with -15: -15 > -20, min stays -20. The global minimum is -20.

Example 3

Input

nums = [7]

Output

7

Explanation: The array contains a single element. By definition, the minimum of a singleton set is the element itself. No comparisons are needed beyond initialization. Return 7.

Example 4

Input

nums = []

Output

Infinity

Explanation: The input array is empty. According to the problem specification, an empty stream has no defined minimum value. The function returns Infinity to represent the upper bound of the real number line, indicating no data was processed.

Constraints

  • 0 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • Time Complexity: O(N)
  • Space Complexity: O(1)

Optimal Approach & Strategy

The optimized approach involves iterating through the array once, keeping track of the smallest element seen so far. This reduces the time complexity to O(N) and the space complexity to O(1).

Brute Force Approach

The brute force approach involves comparing every element with every other element to determine the smallest one. This results in a time complexity of O(N^2), which is inefficient for large datasets.

Verified Code Solutions

JavaScript Solution
Time: O(N)
/**
 * @param {number[]} nums
 * @return {number}
 */
var findMin = function(nums) {
    let minVal = nums[0];
    for (let i = 1; i < nums.length; i++) {
        if (nums[i] < minVal) {
            minVal = nums[i];
        }
    }
    return minVal;
};

Asked in Top Tech Interviews

UberAdobe

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.