KatInterview

Friend Product Suggestions and Click-Path Reachability

Trees & GraphsModel answer
MediumModel answerTrees & Graphshash-setset-differencegraph

Last updated

Two short coding tasks: suggest products that a user's friends bought but the user hasn't, and decide whether one URL can reach another through recorded navigation steps.

Answer both parts below. For each, explain your approach, give code, and state the time and space complexity.

Part 1: Suggest products from friends' purchases

A shopping platform stores these records:

type Purchase = { productId: string; amount: number; purchaseId: string };
type User     = { id: string; friends: string[]; purchases: Purchase[] };
type Product  = { productId: string; productName: string };

Inputs

  • users: every user in the system.
  • products: the catalog, which maps each productId to a productName.
  • targetUserId: the user who should receive suggestions.

Task

Return the names of all products that satisfy both rules:

  1. At least one friend of the target user has bought the product.
  2. The target user has never bought it.

In set terms, with P_friends the product IDs bought by any friend and P_user the product IDs bought by the target, return the names for P_friends − P_user.

Rules

  • Every friend ID is guaranteed to appear in users.
  • A product may appear in many purchases (by one user or several), but it must appear only once in the output.
  • Output order is up to you. State it if you pick one, for example alphabetical by name.

Give the complexity in terms of U (users), F (friends of the target), T (total purchases) and P (distinct products).

Part 2: Can one page lead to another?

You have a log of navigation events with the schema:

(user_id, source_url, destination_url)

Each row says a user went to destination_url directly after source_url. Treat every distinct (source_url, destination_url) pair as a directed edge between URL nodes.

Task

Given url1 and url2, decide whether url2 can be reached from url1 by following one or more edges.

  1. Describe how you would build an in-memory graph from the log.
  2. Implement canReach(url1, url2) returning a boolean.
  3. Give the complexity in terms of V (distinct URLs) and E (distinct source-to-destination pairs).

You may assume the whole log fits in memory.

Hints

Hint 1

Part 1 is a set difference. Index users by id and products by id first so every lookup is constant time, and keep the target's purchased product IDs in a set.

Hint 2

Instead of building the friends' product set and subtracting afterwards, skip any product already in the target's set while you scan each friend's purchases. Duplicates then disappear for free.

Hint 3

In Part 2 the user_id column is irrelevant. Store an adjacency map from each URL to a set of next URLs so repeated clicks do not create duplicate edges, then run a BFS with a visited set.

Hint 4

Read 'one or more edges' carefully: when url1 equals url2 the answer should be true only if url1 sits on a cycle. Seed the search with url1's direct successors and do not pre-mark url1 as visited.