Implement an in-memory social network graph with three operations:
-
follow(followerId, followeeId)
: record that one user follows another user. Duplicate follows should not create duplicate edges. Self-follow can be ignored or rejected, but state your choice.
-
unfollow(followerId, followeeId)
: remove the follow edge if it exists. Removing a non-existent edge should be a no-op.
-
recommend(userId, k)
: return up to
k
recommended users using a friend-of-friend rule. A candidate is any user followed by someone that
userId
follows. Exclude
userId
and users already followed by
userId
.
Rank recommendations by the number of distinct intermediate users that connect userId to the candidate. Break ties deterministically, for example by smaller user id or lexicographic order. Discuss the data structures you would use and the time complexity of each operation.