Quick Overview

This question evaluates algorithm design and data-structure skills—frequency counting, hashing, and handling edge cases such as ties and a no-mode rule—within the coding and algorithms domain for data scientist roles.

Compute array modes with ties and no-mode rule

Company: Amazon

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Write a function that returns the mode(s) of an integer array. Requirements: if all values are unique, return an empty list (there is no mode); allow and return multiple modes, ordered by first occurrence in the input; run in O(n) expected time and O(u) extra space where u is the number of unique values. Follow-ups: 1) How would you support a streaming array with unknown length using O(u_window) space and emit current modes over a sliding window of size W? 2) How would you break ties deterministically in case-insensitive string data while preserving stable order? 3) Analyze worst-case time and space. Examples: [3,1,2,2,3] -> [3,2] (both appear twice); [1,2,3] -> []; [7,7,7] -> [7].

Quick Answer: This question evaluates algorithm design and data-structure skills—frequency counting, hashing, and handling edge cases such as ties and a no-mode rule—within the coding and algorithms domain for data scientist roles.

Return the mode values of an integer array. If every value is unique, return an empty list. If several values tie for highest frequency, return them in first-occurrence order.

Constraints

  • nums may be empty.
  • Return [] when the highest frequency is 1.
  • Multiple modes must preserve first occurrence in the input.

Examples

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

Expected Output: [3, 2]

Explanation: 3 and 2 both occur twice and are ordered by first occurrence.

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

Expected Output: []

Explanation: All values are unique, so there is no mode.

Hints

  1. Count frequencies and remember first-seen order.
  2. The no-mode rule applies only when every frequency is 1.

Loading coding console...