KatInterview

Frequency Peaks and Prime Extraction

Math & Bit ManipulationAuto-graded · 19 test cases
MediumAuto-gradedMath & Bit Manipulationhash-mapsieve-of-eratosthenesfrequency-counting

Last updated

Tests frequency counting with tie-aware mode detection and prime identification via sieve, including edge cases for unique values and non-prime inputs.

Frequency Peaks and Prime Extraction

You are building a statistical utility module that needs two analysis routines over integer arrays. Implement a single function that returns both results in one pass.

Task

Given a list of integers nums, return a dictionary with exactly two keys:

"mode" — the most frequently occurring value(s):

  • If nums is empty, or if every value in nums appears exactly once, set "mode" to None.
  • Otherwise, collect every value tied for the highest frequency. Return them as a sorted list, even when only one value qualifies (e.g., [3] not 3).

"primes" — the prime values present in the list:

  • Identify every value in nums that is a prime number.
  • Return a sorted list of distinct such values.
  • Values less than 2 (including negatives, zero, and one) are never prime and must be excluded.
  • If no primes exist, return an empty list [].

Notes

  • Both sub-problems operate on the same input list but are fully independent of each other.
  • Duplicate values in nums still count as a single entry in "primes" (deduplication required).
  • A list with a single element always has all-unique values, so its "mode" is None.
  • The "mode" result is a list or None — never a bare integer.

Constraints

  • 0 ≤ len(nums) ≤ 10^5
  • -10^6 ≤ nums[i] ≤ 10^6
  • The function must not mutate the input list.
  • Both lists in the output must be sorted in ascending order.

Examples

Example 1

Input: frequency_peaks_and_primes([1, 2, 2, 3, 4, 4, 5])

Output: {'mode': [2, 4], 'primes': [2, 3, 5]}

Example 2

Input: frequency_peaks_and_primes([1, 2, 3, 4, 5])

Output: {'mode': None, 'primes': [2, 3, 5]}

Example 3

Input: frequency_peaks_and_primes([1, 1, 2, 2, 3, 3, 5, 7])

Output: {'mode': [1, 2, 3], 'primes': [2, 3, 5, 7]}

Example 4

Input: frequency_peaks_and_primes([6, 8, 9, 10])

Output: {'mode': None, 'primes': []}

Hints

Hint 1

For the mode, a single pass with a frequency map gives you everything you need: the counts, the maximum count, and whether that maximum is greater than 1.

Hint 2

For primes, consider whether trial division per element or a sieve over the range up to max(nums) is more efficient when values are densely packed — and think about what to do if no element is ≥ 2.

Hint 3

Watch the edge cases: an empty input, a list where every value appears once (mode is None, not an empty list), the number 1 (not prime), and negative numbers (they can be modes but never primes).