Quick Overview

This question evaluates algorithmic problem-solving skills, including frequency analysis for mode computation, correct handling of ties and uniqueness edge cases, and basic number-theoretic competency for prime identification.

Implement Function to Determine Mode and Prime Numbers

Company: Amazon

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

##### Scenario Implementing utility algorithms within a data-processing library. ##### Question Write a function that returns the mode of an integer list; if all values are unique return None, and support multiple modes when ties exist. Given an integer array containing 1 to n, output all prime numbers in the array. ##### Hints Emphasize time complexity and edge-case handling.

Overview: This question evaluates algorithmic problem-solving skills, including frequency analysis for mode computation, correct handling of ties and uniqueness edge cases, and basic number-theoretic competency for prime identification.

Given an integer array nums, implement analyze_numbers(nums) that returns a 2-element list: [modes, primes]. modes is either None (if all values in nums appear exactly once) or a sorted list of all values that have the maximum frequency (support ties). primes is a sorted list of unique prime numbers present in nums (values > 1 only). If nums is empty, return [None, []].

Constraints

  • 0 <= len(nums) <= 200000
  • -10^6 <= nums[i] <= 10^6
  • Return modes as None if and only if every value appears once
  • Return primes as unique values in ascending order
  • 1 and negative numbers are not prime

Hints

  1. Use a frequency map (e.g., collections.Counter) to find the maximum frequency and all values that match it.
  2. If the maximum frequency is 1, return modes = None.
  3. To list primes efficiently, build a sieve up to the maximum positive value in nums and filter the unique values.
  4. Ignore values <= 1 for prime detection.

Loading coding console...

Show the approach

Approach

Count frequencies with a hash map to find the maximum occurrence and collect all values that match it. If the maximum is 1, there is no mode, so return None. For primes, gather unique positive values > 1 and run a Sieve of Eratosthenes up to their maximum to mark primality in O(U log log U), where U is the maximum positive value. Finally, filter the set with the sieve and sort the result.

Time complexity:
O(n + U log log U), where n is len(nums) and U is the maximum positive value in nums
Space complexity:
O(n + U)