KatInterview

Retrieve Minimum and Mode from Array

Arrays & HashingAuto-graded · 22 test cases
MediumAuto-gradedArrays & Hashinghash-mapscountingcomplexity-analysis

Last updated

Tests efficient computation of two simple aggregate statistics using optimal data structures and complexity analysis.

You are given an array of integers. Your task is to return a list containing exactly two values:

  1. The smallest number in the array
  2. The number that appears most frequently in the array

If multiple numbers tie for the highest frequency, return the smallest among them.

Input:

  • nums: a list of integers (may be empty)

Output:

  • A list of two integers [minimum, mode], or an empty list if the input is empty

Constraints:

  • 0 ≤ len(nums) ≤ 10⁵
  • -10⁹ ≤ nums[i] ≤ 10⁹
  • When multiple values share the maximum frequency, return the numerically smallest one

Examples

Example 1

Input: get_min_and_mode([1, 2, 3, 1, 2, 1])

Output: [1, 1]

Example 2

Input: get_min_and_mode([4, -1, 4, 2, -1, -1, 4])

Output: [-1, -1]

Hints

Hint 1

The minimum requires examining every element at least once—can you do better than O(n)?

Hint 2

For the mode, consider how a hash map lets you count frequencies in a single pass.

Hint 3

When breaking ties for the mode, you need both the count and the value itself.