BackmediumBinary SearchUberPaytm

Maximized Network Stream Validator Solution

Problem Statement

You are provided with a strictly unimodal array of integers, stream, representing the throughput capacity of a network pipeline at discrete time intervals. The array is guaranteed to have a single peak element, defined as a value strictly greater than its immediate neighbors. The boundaries of the array are considered to have a throughput of negative infinity, ensuring the peak is always internal or at the edges if the array length is 1.

Your task is to identify the index of this peak element using a binary search strategy. The solution must achieve O(log N) time complexity by leveraging the unimodal property: if the middle element is less than its right neighbor, the peak must lie to the right; otherwise, it lies to the left (or is the middle element itself). Return the index of the peak element in the original array.

Input: An array stream of integers. Output: The integer index of the peak element.

Example 1
Input
stream = [1, 3, 5, 4, 2]
Output
2

Explanation: The array is [1, 3, 5, 4, 2]. The peak is 5 at index 2. Binary search: mid=2, stream[2]=5. Left neighbor stream[1]=3, right neighbor stream[3]=4. Since 5 > 3 and 5 > 4, index 2 is the peak.

Example 2
Input
stream = [10, 9, 8, 7, 6, 5]
Output
0

Explanation: The array is strictly decreasing. The peak is the first element, 10, at index 0. Binary search: mid=2, stream[2]=8. Right neighbor stream[3]=7. Since 8 > 7, the peak is to the left. Narrow to [0,1]. mid=0, stream[0]=10. Right neighbor stream[1]=9. Since 10 > 9, index 0 is the peak.

Example 3
Input
stream = [1, 2, 3, 4, 5, 6, 7]
Output
6

Explanation: The array is strictly increasing. The peak is the last element, 7, at index 6. Binary search: mid=3, stream[3]=4. Right neighbor stream[4]=5. Since 4 < 5, the peak is to the right. Narrow to [4,6]. mid=5, stream[5]=6. Right neighbor stream[6]=7. Since 6 < 7, the peak is to the right. Narrow to [6,6]. mid=6, stream[6]=7. No right neighbor (or -inf), so index 6 is the peak.

Example 4
Input
stream = [42]
Output
0

Explanation: The array has a single element. By definition, it is the peak. Return index 0.

Constraints

  • 1 <= stream.length <= 10^5
  • -10^9 <= stream[i] <= 10^9
  • stream is strictly unimodal: there exists an index k such that stream[0] < stream[1] < ... < stream[k] > stream[k+1] > ... > stream[n-1]
  • All elements in the array are distinct
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 Network Stream Validator — Problem Statement & Solution Guide

Binary SearchMediumSearch Peak Element
TimeO(log n)
|
SpaceO(1)

Problem Description

You are provided with a strictly unimodal array of integers, stream, representing the throughput capacity of a network pipeline at discrete time intervals. The array is guaranteed to have a single peak element, defined as a value strictly greater than its immediate neighbors. The boundaries of the array are considered to have a throughput of negative infinity, ensuring the peak is always internal or at the edges if the array length is 1.

Your task is to identify the index of this peak element using a binary search strategy. The solution must achieve O(log N) time complexity by leveraging the unimodal property: if the middle element is less than its right neighbor, the peak must lie to the right; otherwise, it lies to the left (or is the middle element itself). Return the index of the peak element in the original array.

Input: An array stream of integers.

Output: The integer index of the peak element.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Maximized Network Stream Validator"

medium

WHY DOES IT MATTER?

Peak‑finding via binary search exemplifies the broader pattern of "searching in a monotonic landscape". Recognizing monotonicity lets you discard half of the search space at each step, a skill that translates to many real‑world problems like load‑balancing thresholds, version roll‑outs, and capacity planning.

OPTIMIZATION CHALLENGE

The key insight is that the relative ordering of a middle element and its right neighbor tells you definitively which side the peak cannot be on. This eliminates the need to examine every element and reduces the problem from linear to logarithmic complexity.

REAL-WORLD CONNECTION

In distributed systems, imagine a service whose latency first improves as you add resources, then degrades after saturation. Finding the optimal resource count mirrors locating the peak in a unimodal performance curve, and binary search quickly pinpoints the sweet spot without exhaustive testing.

During the interview, write the loop as "while (low < high)" and update pointers based on the comparison with the right neighbor; this avoids off‑by‑one errors and naturally converges to the peak index.

COMPLEXITY AT A GLANCE

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

Core Theory — Why This Approach?

A strictly unimodal array rises monotonically to a single maximum (the peak) and then falls monotonically. This shape guarantees that for any index i, if stream[i] < stream[i+1] the peak lies to the right, and if stream[i] > stream[i+1] the peak lies to the left or at i. A naive linear scan would examine each element until the peak is found, which costs O(n) time and becomes prohibitive for large streams (e.g., millions of time‑samples). By exploiting the monotonic halves, we can apply a binary‑search‑style divide‑and‑conquer: compare the middle element with its right neighbor and discard the half that cannot contain the peak. Repeating this halving reduces the search space logarithmically, yielding an O(log n) solution while using only O(1) extra space. This optimal paradigm is a classic example of "binary search on answer" where the ordering property, not the exact values, drives the decision.

Interview Questions on This Problem

Q1How would you modify the algorithm if the array could contain multiple equal‑height peaks?

If equal peaks are allowed, the strict > comparison must be changed to >= when moving right; you can still use binary search but you must ensure termination by checking both sides when stream[mid] == stream[mid+1] and possibly fall back to linear scan in the flat region. The worst‑case time degrades to O(n) only when the entire array is flat.

Q2Explain why a simple "find max" using Math.max on the whole array is not acceptable in an interview setting for this problem.

Math.max performs a linear scan, which is O(n) and ignores the guaranteed unimodal property. Interviewers expect candidates to leverage problem constraints to achieve O(log n) time, demonstrating algorithmic insight rather than brute force.

Q3Can you adapt the peak‑finding binary search to work on a virtual stream where you can only query an index via an API call that may fail intermittently?

Wrap each query in a retry mechanism and treat a failed call as a temporary unknown; you can still perform binary search by only proceeding when both mid and mid+1 values are successfully retrieved. If a call fails repeatedly, fallback to a safe linear scan around the uncertain region, preserving overall O(log n) expected time.

Examples

Example 1

Input

stream = [1, 3, 5, 4, 2]

Output

2

Explanation: The array is [1, 3, 5, 4, 2]. The peak is 5 at index 2. Binary search: mid=2, stream[2]=5. Left neighbor stream[1]=3, right neighbor stream[3]=4. Since 5 > 3 and 5 > 4, index 2 is the peak.

Example 2

Input

stream = [10, 9, 8, 7, 6, 5]

Output

0

Explanation: The array is strictly decreasing. The peak is the first element, 10, at index 0. Binary search: mid=2, stream[2]=8. Right neighbor stream[3]=7. Since 8 > 7, the peak is to the left. Narrow to [0,1]. mid=0, stream[0]=10. Right neighbor stream[1]=9. Since 10 > 9, index 0 is the peak.

Example 3

Input

stream = [1, 2, 3, 4, 5, 6, 7]

Output

6

Explanation: The array is strictly increasing. The peak is the last element, 7, at index 6. Binary search: mid=3, stream[3]=4. Right neighbor stream[4]=5. Since 4 < 5, the peak is to the right. Narrow to [4,6]. mid=5, stream[5]=6. Right neighbor stream[6]=7. Since 6 < 7, the peak is to the right. Narrow to [6,6]. mid=6, stream[6]=7. No right neighbor (or -inf), so index 6 is the peak.

Example 4

Input

stream = [42]

Output

0

Explanation: The array has a single element. By definition, it is the peak. Return index 0.

Constraints

  • 1 <= stream.length <= 10^5
  • -10^9 <= stream[i] <= 10^9
  • stream is strictly unimodal: there exists an index k such that stream[0] < stream[1] < ... < stream[k] > stream[k+1] > ... > stream[n-1]
  • All elements in the array are distinct

Optimal Approach & Strategy

Perform a binary search: at each step compare mid with mid+1; if stream[mid] < stream[mid+1] move low to mid+1, else move high to mid. When low equals high, that index is the peak.

Brute Force Approach

Iterate through the array from left to right and return the first index i where stream[i] > stream[i-1] and stream[i] > stream[i+1].

Verified Code Solutions

JavaScript Solution
Time: O(log n)
function solution(nums) {
   let sum = 0;
   for (let num of nums) {
       sum += num;
   }
   return sum;
}

Asked in Top Tech Interviews

UberPaytm

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.