BackeasyGraphsGoogleAmazon

Protocol Tome Partition 25 Solution

Problem Statement

Protocol Tome Partition 25

You are given a sequence of integers that represent protocol and tome metrics collected during a system run. Your task is to identify the metric value that occurs most frequently in the sequence. If several values share the highest frequency, return the smallest numeric value among them. The input consists of an integer n (the number of metrics) followed by n integers. The output should be a single integer – the chosen metric value.

Input The first line contains a single integer n (1 ≤ n ≤ 10^5). The second line contains n space‑separated integers, each in the range [−10^9, 10^9].

Output Print one integer: the metric value that appears most often, breaking ties by selecting the smallest value.

The problem can be solved efficiently by counting frequencies with a hash map and then scanning the map to find the desired value, achieving O(n) time and O(n) auxiliary space.

Example 1
Input
7 1 2 2 3 2 4 5
Output
2

Explanation: The frequencies are: 1→1, 2→3, 3→1, 4→1, 5→1. The maximum frequency is 3, belonging to value 2. Hence the output is 2.

Example 2
Input
5 5 5 4 4 3
Output
4

Explanation: Frequencies: 5→2, 4→2, 3→1. Values 5 and 4 tie with frequency 2. The smallest of these is 4, so the output is 4.

Example 3
Input
6 -1 -1 -2 -2 -3 -3
Output
-3

Explanation: All three values appear twice. The smallest numeric value among them is -3, which is returned.

Example 4
Input
4 10 20 30 40
Output
10

Explanation: Each value appears once. The smallest value is 10, so the output is 10.

Constraints

  • 1 <= n <= 100000
  • -1000000000 <= metric value <= 1000000000
  • The input contains exactly n integers after the first line
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

Protocol Tome Partition 25 — Problem Statement & Solution Guide

GraphsEasyFrequency Hash Map
TimeO(n)
|
SpaceO(k)

Problem Description

Protocol Tome Partition 25

You are given a sequence of integers that represent protocol and tome metrics collected during a system run. Your task is to identify the metric value that occurs most frequently in the sequence. If several values share the highest frequency, return the smallest numeric value among them. The input consists of an integer n (the number of metrics) followed by n integers. The output should be a single integer – the chosen metric value.

Input

The first line contains a single integer n (1 ≤ n ≤ 10^5). The second line contains n space‑separated integers, each in the range [−10^9, 10^9].

Output

Print one integer: the metric value that appears most often, breaking ties by selecting the smallest value.

The problem can be solved efficiently by counting frequencies with a hash map and then scanning the map to find the desired value, achieving O(n) time and O(n) auxiliary space.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Protocol Tome Partition 25"

easy

WHY DOES IT MATTER?

Mode detection with tie‑breaking is a classic frequency‑analysis pattern used in many data‑processing tasks.

OPTIMIZATION CHALLENGE

The key is to avoid sorting or nested loops, reducing the complexity from O(n log n) to O(n).

REAL-WORLD CONNECTION

Log aggregation systems often need to report the most common error code, preferring the lowest code when frequencies match.

Cache the current best candidate while populating the map to eliminate a second pass over the frequencies.

COMPLEXITY AT A GLANCE

⏱ Time:O(n)
💾 Space:O(k)

Core Theory — Why This Approach?

The problem reduces to finding the mode of a multiset of integers with a deterministic tie‑breaker: the smallest value among those with maximal frequency. A hash map (or frequency array when the value range is bounded) lets us count occurrences in linear time, while a naive approach—such as sorting the entire list and then scanning for runs—incurs O(n log n) time and unnecessary overhead for large inputs.

Using a frequency map we can update counts in O(1) amortized per element, then maintain the current best candidate by comparing both frequency and value. This single‑pass paradigm is optimal because any algorithm must inspect each element at least once, establishing a lower bound of Ω(n) time.

Interview Questions on This Problem

Q1How would you handle the tie‑breaking rule when multiple numbers share the highest frequency?

Maintain two variables: maxFreq and answer. When a number's frequency exceeds maxFreq, update both; if it equals maxFreq, set answer to the smaller of the current answer and the number.

Q2What data structure gives O(1) average update and lookup for frequencies?

An unordered hash map (e.g., std::unordered_map or dict) provides constant‑time average operations. It maps each distinct integer to its occurrence count.

Q3Can this problem be solved in O(1) extra space?

Only if the input range is known and small enough to use a fixed‑size array as a frequency bucket. Otherwise, extra space proportional to the number of distinct values is required.

Examples

Example 1

Input

7
1 2 2 3 2 4 5

Output

2

Explanation: The frequencies are: 1→1, 2→3, 3→1, 4→1, 5→1. The maximum frequency is 3, belonging to value 2. Hence the output is 2.

Example 2

Input

5
5 5 4 4 3

Output

4

Explanation: Frequencies: 5→2, 4→2, 3→1. Values 5 and 4 tie with frequency 2. The smallest of these is 4, so the output is 4.

Example 3

Input

6
-1 -1 -2 -2 -3 -3

Output

-3

Explanation: All three values appear twice. The smallest numeric value among them is -3, which is returned.

Example 4

Input

4
10 20 30 40

Output

10

Explanation: Each value appears once. The smallest value is 10, so the output is 10.

Constraints

  • 1 <= n <= 100000
  • -1000000000 <= metric value <= 1000000000
  • The input contains exactly n integers after the first line

Optimal Approach & Strategy

Use a hash map to count frequencies in one pass and update the answer on the fly, achieving O(n) time.

Brute Force Approach

Sort the array and then scan for the longest run of equal values, handling ties by checking the run's start value.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function solution(nums, K) {
   let j = nums.length - 1;
   while (j >= 0 && nums[j] > K) {
       j--;
   }
   return nums[j];
}

Asked in Top Tech Interviews

GoogleAmazonMicrosoft

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.