Social Network
Implement follow, unfollow, snapshot, and is_following. The snapshot captures the current state of all relationships and returns an incremental ID starting from 0. is_following must answer whether user A was following user B at a specific past snapshot.
Extend the class with get_followers(user_id, snapshot_id) and get_followees(user_id, snapshot_id), returning the full list of who was following a user, or who a user was following, at any past snapshot.
Add recommend(user_id, snapshot_id, k=5), which returns the top-K users to follow based on friends-of-friends at a given snapshot. A candidate is someone the user doesn’t already follow but is followed by at least one of their followees.
This problem asks for a versioned social-network design that supports follow/unfollow operations, snapshots, and historical queries. Beyond checking whether one user followed another at a past snapshot, it also requires retrieving all followers and followees at any snapshot, plus a friends-of-friends recommendation function ranked by candidate frequency. The main challenge is storing relationship history efficiently so past-state queries can be answered quickly, often using per-user time-stamped adjacency data or other persistent structures, while recommendation needs careful traversal, deduplication, and ranking.