Find the top 3 products most often bought together with a target product

Quick Overview

Given a list of shopping transactions and a target product, return the three products that share the most transactions with the target, breaking ties by name. It tests hash map counting, handling repeated items within one transaction, and sorting with a composite key.

Find the top 3 products most often bought together with a target product

Company: Shopify

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a purchase history represented as a list of `transactions`, where each transaction is a list of product names, and a specific `target` product, find the top 3 products most frequently bought together with the `target` product. Two products are considered bought together if they appear in the same transaction. Return the top 3 co-occurring products sorted by frequency in descending order. If there is a tie in frequency, sort those products lexicographically. If fewer than 3 products co-occur with the target, return all of them. ### Function Signature ```python def top_cooccurring(transactions: list[list[str]], target: str) -> list[str]: ``` ### Rules - A product's frequency is the number of transactions that contain both it and `target`. A product listed more than once in the same transaction still counts once for that transaction. - The target is never counted as its own partner. - Order by frequency descending, then by name ascending in plain character order (digits sort before letters). - Return at most 3 names. If fewer than 3 products co-occur with the target, return all of them. This can be an empty list when the target only ever appears alone. ### Constraints - `1 <= len(transactions) <= 10^4` - `1 <= len(transactions[i]) <= 100` - `1 <= len(product name) <= 50` - All product names consist of lowercase English letters and digits. - `target` appears in at least one transaction. ### Examples ```text Input: transactions = [["bread","milk","eggs"], ["bread","butter"], ["milk","eggs","bread","butter"], ["bread","juice"], ["milk","bread","juice"]], target = "bread" Output: ["milk","butter","eggs"] ``` All 5 transactions contain "bread". The counts are milk=3, butter=2, eggs=2 and juice=2. Milk comes first, and among the three products tied at 2, butter and eggs come before juice alphabetically, so juice is cut. ```text Input: transactions = [["laptop","mouse"], ["laptop","keyboard","mouse"], ["phone","case"], ["laptop","keyboard"]], target = "laptop" Output: ["keyboard","mouse"] ``` Three transactions contain "laptop", giving keyboard=2 and mouse=2. The tie is broken alphabetically, and only 2 products co-occur, so both are returned. ```text Input: transactions = [["tea","cake","cake"], ["tea","jam"], ["jam","tea"], ["tea"]], target = "tea" Output: ["jam","cake"] ``` Jam shares 2 transactions with tea. Cake is listed twice but only in 1 transaction, so its count is 1. The last transaction has no other product and adds nothing.

Overview: Given a list of shopping transactions and a target product, return the three products that share the most transactions with the target, breaking ties by name. It tests hash map counting, handling repeated items within one transaction, and sorting with a composite key.

Shopify logo
Shopify
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given a purchase history represented as a list of transactions, where each transaction is a list of product names, and a specific target product, find the top 3 products most frequently bought together with the target product.

Two products are considered bought together if they appear in the same transaction.

Return the top 3 co-occurring products sorted by frequency in descending order. If there is a tie in frequency, sort those products lexicographically. If fewer than 3 products co-occur with the target, return all of them.

Function Signature

def top_cooccurring(transactions: list[list[str]], target: str) -> list[str]:

Rules

  • A product's frequency is the number of transactions that contain both it and target . A product listed more than once in the same transaction still counts once for that transaction.
  • The target is never counted as its own partner.
  • Order by frequency descending, then by name ascending in plain character order (digits sort before letters).
  • Return at most 3 names. If fewer than 3 products co-occur with the target, return all of them. This can be an empty list when the target only ever appears alone.

Constraints

  • 1 <= len(transactions) <= 10^4
  • 1 <= len(transactions[i]) <= 100
  • 1 <= len(product name) <= 50
  • All product names consist of lowercase English letters and digits.
  • target appears in at least one transaction.

Examples

Input: transactions = [["bread","milk","eggs"], ["bread","butter"], ["milk","eggs","bread","butter"], ["bread","juice"], ["milk","bread","juice"]], target = "bread"
Output: ["milk","butter","eggs"]

All 5 transactions contain "bread". The counts are milk=3, butter=2, eggs=2 and juice=2. Milk comes first, and among the three products tied at 2, butter and eggs come before juice alphabetically, so juice is cut.

Input: transactions = [["laptop","mouse"], ["laptop","keyboard","mouse"], ["phone","case"], ["laptop","keyboard"]], target = "laptop"
Output: ["keyboard","mouse"]

Three transactions contain "laptop", giving keyboard=2 and mouse=2. The tie is broken alphabetically, and only 2 products co-occur, so both are returned.

Input: transactions = [["tea","cake","cake"], ["tea","jam"], ["jam","tea"], ["tea"]], target = "tea"
Output: ["jam","cake"]

Jam shares 2 transactions with tea. Cake is listed twice but only in 1 transaction, so its count is 1. The last transaction has no other product and adds nothing.