KatInterview

Versioned Social Graph: Follow, Snapshot, and Recommend

Data Structure DesignAuto-graded · 20 test cases
MediumAuto-gradedData Structure Designhash-mapsimulationdesign

Last updated

Tests data-structure design for a directed follow graph with copy-on-snapshot history and friend-of-friend recommendations ranked by mutual count.

Versioned Social Graph

You are building an in-memory social network engine that processes a sequence of operations on a directed follow graph.

Implement a function that accepts a list of operation names and their corresponding arguments, executes each operation in order, and returns a parallel list of results — one entry per operation.


Operations

OperationArgumentsReturns
"follow"[follower, followee]None
"unfollow"[follower, followee]None
"snapshot"[]int — the new snapshot id
"is_following"[follower, followee, snapshot_id]bool
"following"[follower, snapshot_id]List[int]
"recommend"[user, k]List[int]

Behaviour Rules

follow / unfollow

  • Relationships are one-directional: follow(A, B) means A follows B; B does not automatically follow A.
  • A user may not follow themselves. Any follow call where follower == followee is silently ignored and still produces None.
  • Calling follow on a pair that is already connected is a no-op (idempotent).
  • Calling unfollow on a pair that is not connected is a no-op; it does not raise an error.

snapshot

  • Freezes the entire follow graph exactly as it stands at the moment of the call.
  • Snapshot ids are integers assigned in order, starting from 0. The first snapshot call returns 0, the second returns 1, and so on.
  • Any follow or unfollow operations executed after a snapshot has been taken do not alter that snapshot in any way.

is_following

  • Returns True if follower was following followee in the graph at the time snapshot_id was taken; False otherwise.

following

  • Returns a list of all user ids that follower was following at the time snapshot_id was taken.
  • The list must be sorted in strictly ascending order.
  • If follower had no outgoing follows at that snapshot, return an empty list.

recommend

  • Operates on the current live graph, not on any snapshot.
  • The recommendation pool consists solely of users that user does not already follow and that can be reached via at least one friend-of-friend path from user. Concretely: a candidate C is eligible if and only if there exists at least one user V such that user follows V and V follows C, where C ≠ user and user does not already follow C. Users that appear nowhere in the graph, or that have no such mutual connection, are never included in the output.
  • Score each eligible candidate by counting the number of distinct users V satisfying the condition above (i.e., the number of user's followees who also follow C). This is the candidate's mutual-connection count.
  • Sort the eligible candidates: highest mutual-connection count first. Break ties by ascending user id.
  • Return the first k candidates from this sorted list. If fewer than k eligible candidates exist, return all of them.

Constraints

  • All user ids are positive integers.
  • 1 <= k <= 100
  • Snapshot ids passed to "is_following" and "following" are always valid (i.e., a "snapshot" call with that id has already been executed).
  • The total number of operations in one call will not exceed 500.
  • User ids will not exceed 1000.

Examples

Example 1

Input: run_graph_operations(['follow', 'follow', 'snapshot', 'unfollow', 'follow', 'follow', 'snapshot', 'is_following', 'is_following', 'is_following', 'following', 'following'], [[1, 2], [1, 3], [], [1, 2], [2, 4], [3, 4], [], [1, 2, 0], [1, 2, 1], [2, 4, 0], [1, 0], [1, 1]])

Output: [None, None, 0, None, None, None, 1, True, False, False, [2, 3], [3]]

Example 2

Input: run_graph_operations(['follow', 'follow', 'follow', 'follow', 'snapshot', 'recommend'], [[1, 2], [1, 3], [2, 4], [3, 4], [], [1, 5]])

Output: [None, None, None, None, 0, [4]]

Hints

Hint 1

For snapshot, take a full deep copy of the adjacency map at that instant. Under the given constraints this is cheap, and it guarantees that later mutations never bleed into frozen history.

Hint 2

For is_following and following, only consult the frozen snapshot dict — never the live graph. The snapshot already holds the correct state for that point in time.

Hint 3

For recommend, iterate over every account that user follows, then over every account each of those followees follows; accumulate scores in a dict. After the loop, exclude user and anyone user already follows, then sort by (-score, id). Users that never appear in the scores dict are not candidates at all.