Design a recommendation engine that ranks items by Jaccard similarity between users

Quick Overview

Implement a small collaborative-filtering recommender that records user-item interactions, computes Jaccard similarity between users, and ranks unseen items by the summed similarity of other users. It tests set-based data modeling, deterministic tie-breaking, floating-point pitfalls and complexity analysis.

Design a recommendation engine that ranks items by Jaccard similarity between users

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a `RecommendationEngine` that stores user-item interactions and recommends items to a user based on the preferences of similar users (a simple form of collaborative filtering). User similarity is the Jaccard index over the sets of items each user has interacted with: `sim(A, B) = |A ∩ B| / |A ∪ B|`. If both users have no interactions, the similarity is defined as `0.0`. All similarity scores returned by the engine must be rounded to 4 decimal places. Recommendations for a user are items the user has not yet interacted with, ranked by the cumulative similarity of the other users who have interacted with each item. Ties are broken by item id in lexicographic ascending order so that the output is deterministic. Implement the following methods: - `addInteraction(user: str, item: str)`: record that the given user has interacted with the given item. - `getSimilarityIndex(user: str, users: List[str]) -> List[float]`: return the Jaccard similarity between `user` and each user in `users`, aligned with the input order and rounded to 4 decimal places. - `getRecommendations(user: str) -> List[str]`: return the items `user` has not yet interacted with, ordered by cumulative similarity score (descending), with ties broken by item id (ascending). For example, after these calls: ```text addInteraction("alice", "book") addInteraction("alice", "lamp") addInteraction("bob", "book") addInteraction("bob", "lamp") addInteraction("bob", "desk") addInteraction("carol", "lamp") addInteraction("carol", "rug") addInteraction("dave", "sofa") addInteraction("erin", "book") addInteraction("erin", "rug") getSimilarityIndex("alice", ["bob", "carol", "dave", "zoe"]) -> [0.6667, 0.3333, 0.0, 0.0] getRecommendations("alice") -> ["desk", "rug"] ``` bob shares 2 of the 3 items in his union with alice (0.6667). carol and erin each share 1 of 3 (0.3333). dave shares nothing, and zoe has never interacted, so both score 0.0. desk gets 0.6667 from bob, rug gets 0.3333 + 0.3333 = 0.6666 from carol and erin, and sofa is left out because dave's similarity is 0. A tie looks like this: ```text addInteraction("u1", "a") addInteraction("u1", "b") addInteraction("u2", "a") addInteraction("u2", "z") addInteraction("u3", "b") addInteraction("u3", "m") getRecommendations("u1") -> ["m", "z"] ``` u2 and u3 both have similarity 0.3333 with u1, so z and m have the same score and are ordered by id. ```hint Pick the storage Choose a per-user structure that makes two things cheap: the overlap between two users, and the check "has this user already seen the item?". ``` ```hint Equal totals Two items can have the same cumulative score on paper and still compare as unequal in floating point. Think about how to keep the sums exact. ``` ### Constraints & Assumptions - `1 <= number of users <= 10^4` - `1 <= number of items <= 10^4` - User and item ids are non-empty strings of length at most 50. - Every similarity score is rounded to 4 decimal places. - An item's cumulative score is the sum of the rounded similarities of the other users who interacted with it. - Only items with a positive cumulative score are returned. An item that only zero-similarity users interacted with is left out. - Ties in the ranking are broken by item id, ascending. ### Clarifying Questions to Ask - Which rounding rule applies when a similarity falls exactly halfway between two 4-decimal values, as 1/32 = 0.03125 does? - What should each method return for a user id the engine has never seen? - How often is `getRecommendations` called compared with `addInteraction`? That decides whether an index or a cache is worth maintaining. - Does the caller need the full ranked list or only the top k items? ### What a Strong Answer Covers - A storage layout for interactions that makes intersection sizes and "already seen" checks cheap. - A correct Jaccard computation, including the cases where one or both users have no interactions, and the 4-decimal rounding. - A recommendation pass that skips the target user and the items it has already seen, and orders the result deterministically. - How floating-point accumulation interacts with the tie rule, and a way to keep equal totals equal. - The time and space cost of each method at the stated limits. ### Follow-up Questions - How would you avoid comparing the target with every user on each `getRecommendations` call? - How would you support removing an interaction? - How would you return only the top k items without sorting every candidate? - How would you weight recent interactions more heavily than old ones?

Overview: Implement a small collaborative-filtering recommender that records user-item interactions, computes Jaccard similarity between users, and ranks unseen items by the summed similarity of other users. It tests set-based data modeling, deterministic tie-breaking, floating-point pitfalls and complexity analysis.

General logo
General
Nov 24, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a RecommendationEngine that stores user-item interactions and recommends items to a user based on the preferences of similar users (a simple form of collaborative filtering).

User similarity is the Jaccard index over the sets of items each user has interacted with: sim(A, B) = |A ∩ B| / |A ∪ B|. If both users have no interactions, the similarity is defined as 0.0. All similarity scores returned by the engine must be rounded to 4 decimal places.

Recommendations for a user are items the user has not yet interacted with, ranked by the cumulative similarity of the other users who have interacted with each item. Ties are broken by item id in lexicographic ascending order so that the output is deterministic.

Implement the following methods:

  • addInteraction(user: str, item: str) : record that the given user has interacted with the given item.
  • getSimilarityIndex(user: str, users: List[str]) -> List[float] : return the Jaccard similarity between user and each user in users , aligned with the input order and rounded to 4 decimal places.
  • getRecommendations(user: str) -> List[str] : return the items user has not yet interacted with, ordered by cumulative similarity score (descending), with ties broken by item id (ascending).

For example, after these calls:

addInteraction("alice", "book")
addInteraction("alice", "lamp")
addInteraction("bob", "book")
addInteraction("bob", "lamp")
addInteraction("bob", "desk")
addInteraction("carol", "lamp")
addInteraction("carol", "rug")
addInteraction("dave", "sofa")
addInteraction("erin", "book")
addInteraction("erin", "rug")

getSimilarityIndex("alice", ["bob", "carol", "dave", "zoe"])  ->  [0.6667, 0.3333, 0.0, 0.0]
getRecommendations("alice")                                  ->  ["desk", "rug"]

bob shares 2 of the 3 items in his union with alice (0.6667). carol and erin each share 1 of 3 (0.3333). dave shares nothing, and zoe has never interacted, so both score 0.0. desk gets 0.6667 from bob, rug gets 0.3333 + 0.3333 = 0.6666 from carol and erin, and sofa is left out because dave's similarity is 0.

A tie looks like this:

addInteraction("u1", "a")
addInteraction("u1", "b")
addInteraction("u2", "a")
addInteraction("u2", "z")
addInteraction("u3", "b")
addInteraction("u3", "m")

getRecommendations("u1")  ->  ["m", "z"]

u2 and u3 both have similarity 0.3333 with u1, so z and m have the same score and are ordered by id.

Constraints & Assumptions

  • 1 <= number of users <= 10^4
  • 1 <= number of items <= 10^4
  • User and item ids are non-empty strings of length at most 50.
  • Every similarity score is rounded to 4 decimal places.
  • An item's cumulative score is the sum of the rounded similarities of the other users who interacted with it.
  • Only items with a positive cumulative score are returned. An item that only zero-similarity users interacted with is left out.
  • Ties in the ranking are broken by item id, ascending.

Clarifying Questions to Ask Guidance

  • Which rounding rule applies when a similarity falls exactly halfway between two 4-decimal values, as 1/32 = 0.03125 does?
  • What should each method return for a user id the engine has never seen?
  • How often is getRecommendations called compared with addInteraction ? That decides whether an index or a cache is worth maintaining.
  • Does the caller need the full ranked list or only the top k items?

What a Strong Answer Covers Guidance

  • A storage layout for interactions that makes intersection sizes and "already seen" checks cheap.
  • A correct Jaccard computation, including the cases where one or both users have no interactions, and the 4-decimal rounding.
  • A recommendation pass that skips the target user and the items it has already seen, and orders the result deterministically.
  • How floating-point accumulation interacts with the tie rule, and a way to keep equal totals equal.
  • The time and space cost of each method at the stated limits.

Follow-up Questions Guidance

  • How would you avoid comparing the target with every user on each getRecommendations call?
  • How would you support removing an interaction?
  • How would you return only the top k items without sorting every candidate?
  • How would you weight recent interactions more heavily than old ones?