BackmediumHashingAtlassianFlipkart

Verified Stream Minimum Solution

Problem Statement

You are tasked with processing a continuous stream of integer values to determine the 'Verified Stream Minimum'. The verification protocol requires that the minimum value must be confirmed by its frequency in the stream. Specifically, a value $v$ is considered the verified minimum if it is the smallest value in the stream such that its total occurrence count is strictly greater than the occurrence count of any value smaller than it. If no such value exists (i.e., the global minimum has the highest frequency among all values, or is the only value), the global minimum is returned. If the stream is empty, return -1.

Formally, given an array $A$ of length $N$, let $f(x)$ denote the frequency of value $x$ in $A$. Let $S$ be the set of unique values in $A$. The verified minimum is the smallest $v \in S$ such that $f(v) > \max{f(u) \mid u \in S, u < v}$. If the set of values smaller than $v$ is empty, the condition is trivially satisfied. If multiple values satisfy the condition, return the smallest one. Note that the global minimum $m = \min(A)$ always satisfies the condition because there are no values smaller than $m$, so the result is always defined for non-empty streams.

Your task is to implement an efficient algorithm to compute this value. You must process the input array and return the single integer representing the verified stream minimum.

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

Explanation: Frequencies: f(1)=3, f(2)=1, f(3)=2, f(5)=1. Unique values sorted: [1, 2, 3, 5]. Check v=1: No values smaller than 1. Condition holds. Return 1.

Example 2
Input
nums = [10, 10, 10, 2, 2, 1, 1, 1, 1]
Output
1

Explanation: Frequencies: f(1)=4, f(2)=2, f(10)=3. Unique values sorted: [1, 2, 10]. Check v=1: No values smaller than 1. Condition holds. Return 1.

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

Explanation: Frequencies: f(1)=1, f(2)=2, f(4)=4. Unique values sorted: [1, 2, 4]. Check v=1: No values smaller than 1. Condition holds. Return 1.

Example 4
Input
nums = [7, 7, 7, 7, 7, 3, 3, 3, 2, 2, 1, 1, 1, 1, 1, 1]
Output
1

Explanation: Frequencies: f(1)=6, f(2)=2, f(3)=3, f(7)=5. Unique values sorted: [1, 2, 3, 7]. Check v=1: No values smaller than 1. Condition holds. Return 1.

Example 5
Input
nums = [100, 100, 50, 50, 50, 10, 10, 10, 10, 10]
Output
10

Explanation: Frequencies: f(10)=5, f(50)=3, f(100)=2. Unique values sorted: [10, 50, 100]. Check v=10: No values smaller than 10. Condition holds. Return 10.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • The sum of frequencies of all unique values equals nums.length
  • Time complexity must be O(N log N) or better
  • Space complexity must be 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

Verified Stream Minimum — Problem Statement & Solution Guide

HashingMediumFrequency Counter
TimeO(N + M log M)
|
SpaceO(M)

Problem Description

You are tasked with processing a continuous stream of integer values to determine the 'Verified Stream Minimum'. The verification protocol requires that the minimum value must be confirmed by its frequency in the stream. Specifically, a value $v$ is considered the verified minimum if it is the smallest value in the stream such that its total occurrence count is strictly greater than the occurrence count of any value smaller than it. If no such value exists (i.e., the global minimum has the highest frequency among all values, or is the only value), the global minimum is returned. If the stream is empty, return -1.

Formally, given an array $A$ of length $N$, let $f(x)$ denote the frequency of value $x$ in $A$. Let $S$ be the set of unique values in $A$. The verified minimum is the smallest $v \in S$ such that $f(v) > \max\{f(u) \mid u \in S, u < v\}$. If the set of values smaller than $v$ is empty, the condition is trivially satisfied. If multiple values satisfy the condition, return the smallest one. Note that the global minimum $m = \min(A)$ always satisfies the condition because there are no values smaller than $m$, so the result is always defined for non-empty streams.

Your task is to implement an efficient algorithm to compute this value. You must process the input array and return the single integer representing the verified stream minimum.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Verified Stream Minimum"

medium

WHY DOES IT MATTER?

The pattern combines frequency counting with ordered traversal, a common motif in problems where a quantitative property must be compared across a sorted domain (e.g., finding the first dominant element). Mastery of this pattern enables candidates to turn seemingly O(N^2) comparisons into linear‑or‑logarithmic solutions.

OPTIMIZATION CHALLENGE

The key insight is decoupling counting from ordering. By aggregating frequencies in O(N) and then sorting only the distinct keys (which are far fewer than N in many cases), we avoid repeated scans over the entire stream for each candidate, collapsing the quadratic blow‑up to near‑linear time.

REAL-WORLD CONNECTION

Think of a real‑time analytics dashboard that tracks product sales: you need the cheapest product whose sales volume outpaces every cheaper product. The dashboard first aggregates sales per SKU (hash map) and then scans price‑sorted SKUs to surface the qualifying item—exactly the same two‑phase workflow.

When coding, first implement the frequency map with an unordered_map, then extract keys into a vector and sort. Keep a running maxFreq variable while iterating; as soon as you find freq[key] > maxFreq, return that key. Early exit prevents unnecessary work on the tail of the sorted list.

COMPLEXITY AT A GLANCE

⏱ Time:O(N + M log M)
💾 Space:O(M)

Core Theory — Why This Approach?

The Verified Stream Minimum problem asks for the smallest integer v in a multiset such that the frequency of v strictly exceeds the frequency of every integer smaller than v. A naïve solution would recompute frequencies for each candidate by scanning the entire stream, leading to O(N^2) time for N elements—clearly infeasible for large inputs. The optimal paradigm separates counting from ordering: first, a single pass builds a hash map of frequencies (O(N) time, O(M) space where M is the number of distinct values). Second, the distinct keys are sorted, which costs O(M log M). A linear scan over the sorted keys maintains the maximum frequency seen so far among smaller values; the first key whose own frequency beats this maximum is the answer. This two‑phase approach leverages the fact that frequency information is associative and can be aggregated independently of order, while sorting provides the necessary monotonic view to enforce the "smaller‑than" relationship efficiently.

Interview Questions on This Problem

Q1How would you modify the solution if the stream is infinite and you must report the verified minimum after each new element in O(log M) time?

Maintain a balanced binary search tree (e.g., std::map) keyed by the integer value, each node storing its frequency and the maximum frequency in its left subtree (augmented data). Upon each insertion, update the node’s frequency and propagate the left‑max values up the tree. The verified minimum is the leftmost node where its frequency > left‑max of its left child, which can be found by walking down the tree in O(log M).

Q2Explain why a simple max‑heap of frequencies cannot directly solve the problem.

A max‑heap gives the value with the highest overall frequency, but the verified minimum condition depends on a relative comparison with *all smaller values*, not just the global maximum. A value with the highest frequency might be large, while a smaller value with slightly lower frequency could still satisfy the condition if all values below it have even lower counts. Hence, heap ordering alone loses the necessary ordering information.

Q3In a distributed system where the stream is sharded across multiple nodes, how would you compute the verified minimum efficiently?

Each node computes a local frequency map for its shard and sends (value, localCount) pairs to a coordinator. The coordinator aggregates counts per value (summing across nodes) using a hash map, then performs the same sort‑and‑scan step. Because aggregation is associative and commutative, this reduces network traffic to O(M) messages and retains the O(N + M log M) overall complexity.

Examples

Example 1

Input

nums = [5, 3, 3, 1, 1, 1, 2]

Output

1

Explanation: Frequencies: f(1)=3, f(2)=1, f(3)=2, f(5)=1. Unique values sorted: [1, 2, 3, 5]. Check v=1: No values smaller than 1. Condition holds. Return 1.

Example 2

Input

nums = [10, 10, 10, 2, 2, 1, 1, 1, 1]

Output

1

Explanation: Frequencies: f(1)=4, f(2)=2, f(10)=3. Unique values sorted: [1, 2, 10]. Check v=1: No values smaller than 1. Condition holds. Return 1.

Example 3

Input

nums = [4, 4, 4, 4, 2, 2, 1]

Output

4

Explanation: Frequencies: f(1)=1, f(2)=2, f(4)=4. Unique values sorted: [1, 2, 4]. Check v=1: No values smaller than 1. Condition holds. Return 1.

Example 4

Input

nums = [7, 7, 7, 7, 7, 3, 3, 3, 2, 2, 1, 1, 1, 1, 1, 1]

Output

1

Explanation: Frequencies: f(1)=6, f(2)=2, f(3)=3, f(7)=5. Unique values sorted: [1, 2, 3, 7]. Check v=1: No values smaller than 1. Condition holds. Return 1.

Example 5

Input

nums = [100, 100, 50, 50, 50, 10, 10, 10, 10, 10]

Output

10

Explanation: Frequencies: f(10)=5, f(50)=3, f(100)=2. Unique values sorted: [10, 50, 100]. Check v=10: No values smaller than 10. Condition holds. Return 10.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • The sum of frequencies of all unique values equals nums.length
  • Time complexity must be O(N log N) or better
  • Space complexity must be O(N)

Optimal Approach & Strategy

Build a frequency map in one pass, sort the distinct keys, and linearly scan while tracking the maximum frequency of all previously seen (smaller) keys.

Brute Force Approach

For each distinct value, scan the entire stream to count its occurrences and also count occurrences of every smaller value, then pick the smallest that satisfies the condition.

Verified Code Solutions

JavaScript Solution
Time: O(N + M log M)
/**
 * @param {number[]} nums
 * @return {number}
 */
var verifiedStreamMinimum = function(nums) {
    const freq = new Map();
    
    for (const num of nums) {
        freq.set(num, (freq.get(num) || 0) + 1);
    }
    
    let minVal = Infinity;
    let minFreq = 0;
    
    for (const [val, count] of freq) {
        if (count > minFreq || (count === minFreq && val < minVal)) {
            minFreq = count;
            minVal = val;
        }
    }
    
    return minVal;
};

Asked in Top Tech Interviews

AtlassianFlipkart

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.