← Questions

Products your friends bought that you haven't

0
AmazonCodingLast asked Oct 2025

A coding interview question asked in Amazon's coding round, last asked Oct 2025. It tests hashing and graphs.

Recommend products to a user from what their friends bought. Each user has a list of friends and a list of purchases; each purchase has a product id, an amount and a purchase id, and a catalog maps product ids to names. Return the names of the products that at least one of the user's friends bought and the user never bought.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • User(id: str, friends: list[str], purchases: list[Purchase]), Purchase(product_id: str, amount: float, purchase_id: str), Product(product_id: str, name: str).
  • friend_recommendations(users: list[User], products: list[Product], target_id: str) -> list[str] — each product name once, sorted alphabetically.

Example

users:
  "u1": friends ["u2", "u3"], bought p1
  "u2": friends ["u1"],       bought p1, p2, p2
  "u3": friends ["u1"],       bought p3
products: p1 "Kettle", p2 "Mug", p3 "Tea"

friend_recommendations(users, products, "u1")  ->  ["Mug", "Tea"]
# friends bought p1, p2, p3; u1 already has p1; p2 appears twice but is listed once

Constraints

  • U users, the target has F friends, T purchases in total, P distinct products.
  • Every friend id exists among the users; a user can buy the same product many times.

Clarifying questions

  • Is friendship mutual, or is friends one-directional?
  • What order should the result be in: by name, or by how many friends bought it?
  • Can a product id be missing from the catalog?

What a strong answer covers

  • An index from user id to user built once (O(U)), the target's product ids as a set, and a set of product ids over the friends' purchases; the answer is the set difference, mapped to names through a product-id index.
  • Complexity: O(U + T_target + T_friends + P) time, where T_friends counts only the friends' purchases, and O(P) extra space.
  • Duplicates handled by sets, not by deduplicating names (two products can share a name).
  • Edge cases: no friends, a friend with no purchases, an unknown product id.

Follow-ups

  • Rank the recommendations by the number of friends who bought each product, then by total amount.
  • Include friends of friends, with a lower weight.
  • The purchase data is too large for one machine. How would you compute this for every user every night?

Where it's asked

  • Amazon · Coding · Last asked Oct 202555 min · Usually two rounds; one is often an object-oriented design built in steps · No AI

What it tests

hashing · graphs

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding