Implement follow graph with snapshots and recommendations
OpenAISoftware Engineer · Technical ScreenLast updated
This question evaluates design and implementation skills for mutable graph data structures with point-in-time snapshotting and recommendation ranking, testing knowledge of data structures, versioning, graph traversal, and algorithmic time/space complexity analysis.
Design and implement an in-memory “social network” component that supports following/unfollowing, point-in-time (snapshot) queries, and a simple recommendation API.
You are given users identified by integers 1..N (assume N can be large). Implement the following operations:
follow(u, v): userustarts following userv.unfollow(u, v): userustops following userv.snap() -> sid: creates a snapshot of the current follow graph and returns a monotonically increasing snapshot idsid.isFollowing(u, v, sid) -> bool: returns whetheruwas followingvat the time snapshotsidwas taken.recommend(u, k) -> list[v]: recommend up tokusers thatudoes not currently follow (and excludingu). Rank candidates by the number of users thatucurrently follows who also follow the candidate (i.e., “friends-of-friends” count). Break ties by smaller user id.
Clarifications/assumptions you should state and handle:
- Repeated
follow(u, v)when already following should be a no-op; similarlyunfollowwhen not following. - Snapshots are immutable;
isFollowingmust reflect state at that snapshot. - Analyze time and space complexity of your approach, especially for large numbers of operations and snapshots.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.