Top K Most Viewed Profiles With Alphabetical Tie Breaks

Last updated: November 29, 2026

Quick Overview

Given a list of profile view events (each a member id string) and an integer k, return the k most viewed member ids, ordered by view count descending and then by id ascending for ties.

LinkedIn
Coding & Algorithms
Software Engineer
LinkedIn
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


Given a list of profile view events (each a member id string) and an integer k, return the k most viewed member ids, ordered by view count descending and then by id ascending for ties.

A quick screen question modeled on the 'Who viewed your profile' and trending creators surfaces. The interviewer checks counting with a hash map, correct tie ordering, and whether you can explain when a size-k heap beats a full sort.

What the Interviewer Expects
  • Counts views with a hash map or Counter
  • Gets ordering right: count descending, id ascending
  • Uses heapq.nsmallest with a composite key or a size-k heap
  • Compares O(n log k) heap against O(n log n) sort
  • Handles k larger than the number of distinct ids
Key Topics to Cover
hash map
heap
top-k
tie breaking
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How do you do this over a sliding 24 hour window?
  • How would you compute approximate top-k in a stream with bounded memory?
  • How would you merge top-k results from many shards?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: views list of ids, k >= 0. Output: up to k ids. Ties on count are broken by lexicographically smaller id first. If there are fewer than k distinct ids, return all of them....

High-Level Design

Count with Counter. Then select the k smallest items under the key (-count, id), which is the same as the largest counts with ascending id ties. heapq.nsmallest keeps a heap of size k internally...


Submit Your Answer
Markdown supported