You are asked to design and implement an in-memory SocialNetwork class that supports users following each other and creating snapshots of the follow graph.
Implement an API similar to:
social_network = SocialNetwork()
social_network.add_user("A")
social_network.add_user("B")
social_network.follow("A", "B")
snapshot = social_network.create_snapshot()
assert snapshot.is_follow("A", "B")
A snapshot represents an immutable view of the follow relationships at the moment it was created.
snapshot.is_follow(u, v)
which returns whether user
u
follows user
v
in that snapshot
.
For a given snapshot and user:
Given a snapshot, a user u, and an integer k, recommend up to k users for u to follow using this logic:
u
already follows.
u
’s followees.
k
candidates by this count.
u
or
v
does not exist.
follow
calls are idempotent.
u
already follows from recommendations.
Describe your design (data structures, snapshot strategy, complexity) and any edge cases.
Login required