Find top co-viewed products
Company: eBay
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
Quick Answer: 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.
Input: ([], 5, 3)
Expected Output: []
Explanation: There are no sessions, so there are no recommendations.
Input: ([[-1, 10, -1, 5], [10, -2, -2], [-1, -2, 10]], 10, 2)
Expected Output: [-2, -1]
Explanation: Counts are: -1 -> 3, -2 -> 3, 5 -> 1. Since -2 and -1 tie, the smaller product ID -2 comes first.
Input: ([[1, 1], [2, 3], [4]], 9, 2)
Expected Output: []
Explanation: No session contains the target product 9, so nothing is counted.
Input: ([[1, 2], [2, 3]], 2, 0)
Expected Output: []
Explanation: Even though there are co-viewed products, requesting k = 0 means the result must be empty.
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.