DSA interview question
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).
pair<int, int> highestAndLowestFrequency(const vector<int>& arr); // {most frequent, least frequent}; arr non-empty
Input arr | Output | Explanation |
|---|---|---|
[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.)
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 interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in