PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCareers

Quick 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.

  • hard
  • eBay
  • Coding & Algorithms
  • Software Engineer

Find top co-viewed products

Company: eBay

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Onsite

You are given a list of user browsing sessions, where each session is a list of product IDs, a `target_product`, and an integer `k`. A product should be counted only from sessions that contain `target_product` at least once. In every such session, count every occurrence of every other product in that session, and ignore the target product itself. Then return the `k` product IDs with the highest total co-view counts as recommendations. If there are fewer than `k` distinct products, return all of them. If two products have the same frequency, return the smaller product ID first. Design an efficient algorithm for this recommendation task and discuss how you would structure the code for production-quality Java.

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.

You are given a list of user browsing sessions, where each session is a list of product IDs, a target_product, and an integer k. A session contributes to the recommendation counts only if it contains target_product at least once. For every such session, count every occurrence of every other product in that session, and ignore the target product itself. Return the k product IDs with the highest total co-view counts. If there are fewer than k distinct co-viewed products, return all of them. If two products have the same frequency, return the smaller product ID first. As a follow-up, be prepared to explain how you would structure this logic in production-quality Java, including validation, separation of concerns, and unit testing.

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

  1. First determine whether a session should contribute at all: only sessions containing the target product matter.
  2. Use a hash map to accumulate frequencies, then sort products by descending count and ascending product ID.
Last updated: May 7, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • Careers
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Format Words into Fixed-Width Lines - eBay (medium)
  • Assign Ads to Browser Positions - eBay (medium)
  • Solve Dependency, Prefix, and Cache Problems - eBay (medium)
  • Implement an In-Memory File System - eBay (medium)
  • Implement bitmap-based block allocator - eBay (medium)