← Questions
Products your friends bought that you haven't
0
I was asked this
No reports yet. Sign in to say where you were asked it.
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
-
Uusers, the target hasFfriends,Tpurchases in total,Pdistinct products. - Every friend id exists among the users; a user can buy the same product many times.
Clarifying questions
- Is friendship mutual, or is
friendsone-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_friendscounts 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
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
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
