#461 Top K Most Frequent Numbers

Companies:
Itone
Themes:
AlgorithmsHash TablesPractice

The input is a list of integers that may contain null values, which must be ignored.

Return a list of the k most frequent numbers, sorted:

  • By frequency in descending order;
  • When frequencies are equal, by the number itself in ascending order.

Output:

  • If numbers == null or the list is empty, return an empty list;
  • If k <= 0, return an empty list;
  • If there are fewer unique numbers than k, return all of them.

Question review

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in