KatInterview

Versioned Follow Graph with Time-Travel Queries and Suggestions

Data Structure DesignModel answer
MediumModel answerData Structure Designgraphsnapshotsbinary-search

Last updated

Build an in-memory directed follow graph with cheap snapshots, historical edge lookups, and a friends-of-friends suggestion ranking with id tie-breaks.

Versioned Follow Graph

Build an in-memory component that tracks who follows whom among users numbered 1..N. N may be very large, so memory must not scale with N. The component must also answer questions about earlier states of the graph and suggest accounts to follow.

Implement these operations:

  1. follow(u, v): u starts following v.
  2. unfollow(u, v): u stops following v.
  3. snap() -> sid: freeze the current graph and return a snapshot id. Ids increase monotonically.
  4. isFollowing(u, v, sid) -> bool: was u following v at the moment snapshot sid was taken?
  5. recommend(u, k) -> list[int]: return up to k accounts for u to follow.
    • Candidates must not be u and must not be accounts u currently follows.
    • A candidate's score is the number of accounts that u currently follows that also follow the candidate.
    • Sort by score descending, then by user id ascending.

Rules to handle

  • Edges are directed. u following v says nothing about v following u.
  • A repeated follow of an existing edge, or an unfollow of a missing edge, changes nothing.
  • A user cannot follow themselves, so follow(u, u) is ignored.
  • Snapshots are immutable. Later changes never alter the answer for an earlier sid.
  • recommend uses the current graph, not a snapshot, and only returns candidates with a score of at least 1.
  • Decide and state what happens when sid has not been issued yet.

What to deliver

Describe your data structures, implement the five operations, and analyze time and space. Pay particular attention to workloads with millions of mutations and many snapshots.

Example

follow(1, 2); follow(1, 3)
follow(2, 4); follow(3, 4); follow(3, 5)
s0 = snap()                  # 0
unfollow(1, 2)
s1 = snap()                  # 1
isFollowing(1, 2, s0)        # True
isFollowing(1, 2, s1)        # False
recommend(1, 5)              # [4, 5]  (4 via 3 only now; 5 via 3)

After unfollow(1, 2), user 1 follows only 3, so both 4 and 5 score 1 and the tie is broken by id.

Hints

Hint 1

Copying the whole graph on every snapshot costs O(edges x snapshots) memory. Think about recording only what changes, per edge, and tagging each change with a version.

Hint 2

For one edge (u, v), keep a list of (version, present?) entries in increasing version order. A historical query becomes 'find the last entry with version <= sid', which is a binary search. If several changes happen between two snaps, only the last one is observable, so you can overwrite instead of appending.

Hint 3

For recommendations, iterate over the accounts u follows, then over the accounts each of those follows, and count occurrences in a hash map while skipping u and existing followees. Then pick the top k with a heap keyed on (-count, id) instead of sorting everything.