Find top co-viewed products
Company: eBay
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
Overview: This question evaluates algorithmic problem-solving and implementation skills in the Coding & Algorithms domain, including frequency aggregation, ordering with tie-breakers, and efficient data-structure use for session-based recommendation counting.
Constraints
- 0 <= len(sessions) <= 100000
- 0 <= total number of product occurrences across all sessions <= 200000
- Product IDs are integers
- 0 <= k
- Count duplicate occurrences of non-target products; do not count the target product itself
Examples
Input: ([[1, 2, 3], [2, 3, 3, 4], [5, 2, 5], [7, 8]], 2, 3)
Expected Output: [3, 5, 1]
Explanation: Only the first three sessions contain 2. Their co-view counts are: 3 -> 3, 5 -> 2, 1 -> 1, 4 -> 1. The top 3 products are [3, 5, 1].
Input: ([[9, 1, 9, 2, 2], [3, 9, 3], [4, 4], [9]], 9, 5)
Expected Output: [2, 3, 1]
Explanation: Qualifying sessions are the 1st, 2nd, and 4th. Counts are: 2 -> 2, 3 -> 2, 1 -> 1. There are fewer than 5 distinct co-viewed products, so return all of them, breaking the tie between 2 and 3 by smaller product ID.
Hints
- First determine whether a session should contribute at all: only sessions containing the target product matter.
- Use a hash map to accumulate frequencies, then sort products by descending count and ascending product ID.