BackmediumTreesPhonePeSalesforce

Adaptive Stream Minimum Solution

Problem Statement

You are given an array of integers, possibly empty. Determine the smallest and largest values present in the array and return their sum. If the array contains no elements, the result should be 0. The task requires a single pass through the data to identify the minimum and maximum efficiently.

Example 1
Input
[3, 1, 4, 1, 5]
Output
6

Explanation: The minimum value is 1 and the maximum is 5. Their sum is 1 + 5 = 6.

Example 2
Input
[-2, -7, 0, 5]
Output
-2

Explanation: The minimum value is -7 and the maximum is 5. Their sum is -7 + 5 = -2.

Example 3
Input
[]
Output
0

Explanation: The array is empty, so by definition the sum of the minimum and maximum is 0.

Example 4
Input
[10]
Output
20

Explanation: With only one element, the minimum and maximum are both 10. Their sum is 10 + 10 = 20.

Constraints

  • 0 <= nums.length <= 100000
  • -1000000000 <= nums[i] <= 1000000000
  • Time complexity must be O(n)
  • Space complexity must be 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

Adaptive Stream Minimum — Problem Statement & Solution Guide

TreesMediumDepth-First Search
TimeO(n)
|
SpaceO(1)

Problem Description

You are given an array of integers, possibly empty. Determine the smallest and largest values present in the array and return their sum. If the array contains no elements, the result should be 0. The task requires a single pass through the data to identify the minimum and maximum efficiently.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Adaptive Stream Minimum"

medium

WHY DOES IT MATTER?

The min‑max pattern is essential because it guarantees linear time and constant space, which is critical for large datasets and real‑time systems where latency and memory usage directly impact performance and cost.

OPTIMIZATION CHALLENGE

The key insight is that you only need to compare each element once against two stored values, eliminating the need for sorting or nested comparisons, thus reducing time from O(n log n) or O(n^2) to O(n).

REAL-WORLD CONNECTION

In load‑balancing, each server reports its current load; a central monitor keeps track of the minimum and maximum loads to decide where to route new requests. This mirrors the single‑pass min‑max update without storing all loads.

When coding, initialize min and max with the first element to avoid special cases for empty arrays, and remember to handle the empty input explicitly before the loop.

COMPLEXITY AT A GLANCE

⏱ Time:O(n)
đŸ’Ÿ Space:O(1)

Core Theory — Why This Approach?

The core of this problem is the classic "min‑max" pattern, which can be solved in a single linear pass by maintaining two variables: one for the current minimum and one for the current maximum. A naive approach might sort the array (O(n log n)) or use nested loops to compare every pair (O(n^2)), both of which become prohibitively expensive as the input size grows. By updating the min and max on the fly—comparing each element to the current extremes and replacing them when a new extreme is found—we achieve optimal time complexity of O(n) and constant auxiliary space, making the algorithm scalable to very large streams of data.

This pattern is a textbook example of the "divide and conquer" principle applied in a trivial form: we split the problem into two independent sub‑problems (finding the smallest and the largest) and then combine their results (by summing). Because the two sub‑problems can be solved in parallel within the same loop, we avoid any additional passes or data structures, which is why the algorithm remains efficient even when the input is a continuous stream or a massive file that cannot fit into memory.

In distributed systems, a similar idea is used when aggregating metrics across shards: each shard computes its local min and max, and the coordinator aggregates these two values to produce the global result. This mirrors the single‑pass approach and demonstrates why the min‑max pattern is both theoretically sound and practically useful.

Interview Questions on This Problem

Q1How would you handle finding the min and max in a data stream that can be larger than memory, as seen in big data platforms like Hadoop or Spark?

In a streaming context, you would maintain two variables for the current min and max and update them as each record arrives. If the stream is partitioned across nodes, each node computes local min/max and then a reduce step aggregates these to global min/max, ensuring O(1) per record and O(log n) communication overhead.

Q2What edge cases should you consider when implementing this algorithm in a fintech application that processes transaction amounts?

You must handle empty input (return 0), very large or very small values that could cause integer overflow when summing, and negative numbers—ensuring the initial min is set to +∞ and max to -∞ or using the first element as the seed.

Q3In a high‑growth startup interview, how would you explain the trade‑off between using a single pass versus sorting the array to find min and max?

Sorting gives O(n log n) time and O(n) space, which is unnecessary for min/max. A single pass is O(n) time and O(1) space, making it more efficient and scalable, especially when the array is large or arrives as a stream.

Examples

Example 1

Input

[3, 1, 4, 1, 5]

Output

6

Explanation: The minimum value is 1 and the maximum is 5. Their sum is 1 + 5 = 6.

Example 2

Input

[-2, -7, 0, 5]

Output

-2

Explanation: The minimum value is -7 and the maximum is 5. Their sum is -7 + 5 = -2.

Example 3

Input

[]

Output

0

Explanation: The array is empty, so by definition the sum of the minimum and maximum is 0.

Example 4

Input

[10]

Output

20

Explanation: With only one element, the minimum and maximum are both 10. Their sum is 10 + 10 = 20.

Constraints

  • 0 <= nums.length <= 100000
  • -1000000000 <= nums[i] <= 1000000000
  • Time complexity must be O(n)
  • Space complexity must be O(1)

Optimal Approach & Strategy

Traverse the array once, updating two variables for the current minimum and maximum, then return their sum. This uses O(n) time and O(1) space.

Brute Force Approach

A naive method would sort the array and then pick the first and last elements, or use nested loops to compare every pair, both of which are inefficient for large inputs.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function solution(nums) {
      if (nums.length === 0) {
         return 0;
      }
      let min = Math.min(...nums);
      let max = Math.max(...nums);
      return min + max;
   }

Asked in Top Tech Interviews

PhonePeSalesforce

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.