PPractick

DSA interview question

Highest and Lowest Occurring Elements in an Array

Easy · Algorithms · Topics: hashing, array

The problem

A music-streaming dashboard wants two numbers from today's play log (an array of song IDs): the most-played song and the least-played song among those that were played at all. When several songs tie, the dashboard shows the one with the smallest ID so the answer is deterministic. It's a direct follow-on from counting frequencies: once you have a frequency table, both answers come from a single pass over it, as long as you get the tie-breaking right.

Example: [10, 5, 10, 15, 10, 5] → most frequent 10 (3 times), least frequent 15 (once).

Function signature

pair<int, int> highestAndLowestFrequency(const vector<int>& arr);  // {most frequent, least frequent}; arr non-empty

Test cases

Input arrOutputExplanation
[10, 5, 10, 15, 10, 5]{10, 15}10 appears 3 times, 15 once
[2, 2, 3, 4, 4, 2]{2, 3}2 appears 3 times; 3 once, 4 twice
[1, 2, 3]{1, 1}All tie at 1, so the smallest value wins both
[7, 7, 1, 1]{1, 1}Tie at 2 for both; smallest value 1
[42]{42, 42}Single element

(Confirm: the array is non-empty; ties are broken by the smallest value; "least frequent" only considers values that appear.)

How interviewers phrase it

Clarifying questions to ask

  1. How should ties be broken: smallest value, first occurrence, or any?
  2. Does "least frequent" mean among values present in the array?
  3. What's the value range? (Array vs hash map.)
  4. Is the array guaranteed non-empty?

Follow-ups to expect

  1. "Can you do it in one pass over the array?"
  2. "What if the element must appear more than N/2 times?"
  3. "How would you return the k most frequent?"
  4. "Complexity?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in