This question evaluates proficiency in data-structure manipulation (set operations, mapping and deduplication), directed-graph modeling and traversal for reachability, and algorithmic time/space complexity analysis.
Implement solutions for the following two data-structure / algorithm problems.
You are given three entity types:
type Purchase = {
productId: string;
amount: number;
purchaseId: string;
};
type User = {
id: string;
friends: string[]; // list of friend userIds
purchases: Purchase[];
};
type Product = {
productId: string;
productName: string;
};
Input
users
: array of all
User
objects in the system.
products
: array of all
Product
objects (mapping
productId -> productName
).
targetUserId
: the ID of the current user we want recommendations for.
Each Purchase references a productId. A user may have multiple purchases of the same product.
Task
Write a function that returns the set/list of product names such that:
Formally:
F
= set of friends of
targetUser
.
P_friends
= set of productIds purchased by any user in
F
.
P_user
= set of productIds purchased by
targetUser
.
productName
for all productIds in
P_friends − P_user
.
Additional details/assumptions:
friends
user IDs exist in the
users
array.
productName
).
Questions
U = number of users
,
F = number of friends of target user
,
T = total number of purchases
, and
P = number of distinct products
.
You are given a dataset (or table) of navigation events with schema:
(user_id, source_url, destination_url)
Each record means that a user visited destination_url immediately after source_url. We treat each (source_url, destination_url) pair as a directed edge in a graph whose nodes are URLs.
Input
url1
and
url2
.
Task
Determine whether it is possible to navigate from url1 to url2 by following one or more edges implied by the table (i.e., whether url2 is reachable from url1 in the directed URL graph).
You should:
boolean canReach(String url1, String url2)
, that returns
true
if there is a path from
url1
to
url2
, and
false
otherwise.
V = number of distinct URLs
and
E = number of edges (distinct source→destination pairs)
.
You may assume that the full set of records fits in memory for the purpose of this problem.