Versioned Social Network
Learn this problemProblem statement
A directed social network has n users numbered from 0 to n - 1. A relationship u -> v means that user u follows user v.
Relationships change over time. The network can create immutable snapshots and later answer whether a relationship existed at a particular snapshot. A snapshot must not copy the entire graph. Instead, keep a history of changes for each directed relationship.
Every snapshot includes all preceding updates. Later updates never change an earlier snapshot. If the same relationship changes more than once before the next snapshot, that snapshot records its state after the last such change.
Operations
Process the commands in order:
FOLLOW u v: add the directed relationshipu -> v. The relationship is guaranteed to be inactive immediately before this command.UNFOLLOW u v: remove the directed relationshipu -> v. The relationship is guaranteed to be active immediately before this command.SNAPSHOT: save the current network state. Snapshot IDs start at0and increase by1. Append the new ID to the output.IS_FOLLOWING u v snapshot_id: appendtrueifufollowedvwhen that snapshot was created; otherwise appendfalse. A relationship that had not yet been followed is inactive.
Only SNAPSHOT and IS_FOLLOWING produce output. Return their outputs in command order as strings.
Performance Requirements
FOLLOW, UNFOLLOW, and SNAPSHOT must each run in expected O(1) time. A historical query for one relationship must run in O(log h) time, where h is the number of recorded changes for that relationship. The total auxiliary space must be O(c), where c is the number of follow and unfollow commands.
Further Interview Stages
The reports also described listing relationships at a snapshot, recommending friends, and comparing two snapshots. The exact recommendation rule and snapshot-difference output were not provided.
Function
processVersionedSocialNetwork(n: int, operations: String[]) → String[]Examples
Example 1
n = 4operations = ["FOLLOW 0 1", "FOLLOW 0 2", "SNAPSHOT", "UNFOLLOW 0 1", "SNAPSHOT", "IS_FOLLOWING 0 1 0", "IS_FOLLOWING 0 1 1", "IS_FOLLOWING 0 2 1"]return = ["0", "1", "true", "false", "true"]Snapshot 0 contains both relationships from user 0. Snapshot 1 is created after 0 unfollows 1, while the relationship 0 -> 2 remains active.
Example 2
n = 3operations = ["FOLLOW 1 2", "UNFOLLOW 1 2", "FOLLOW 1 0", "SNAPSHOT", "IS_FOLLOWING 1 2 0", "IS_FOLLOWING 1 0 0"]return = ["0", "false", "true"]Both changes to 1 -> 2 occur before the first snapshot, so its final state in snapshot 0 is inactive. The relationship 1 -> 0 is active.
Example 3
n = 3operations = ["SNAPSHOT", "FOLLOW 2 1", "SNAPSHOT", "UNFOLLOW 2 1", "FOLLOW 2 1", "SNAPSHOT", "IS_FOLLOWING 2 1 0", "IS_FOLLOWING 2 1 1", "IS_FOLLOWING 2 1 2"]return = ["0", "1", "2", "false", "true", "true"]The relationship is absent in snapshot 0 and present in snapshot 1. It is removed and restored before snapshot 2, so its snapshot 2 state is active.
Constraints
1 <= n <= 1000001 <= operations.length <= 2000000 <= u, v < nandu != v- Every
FOLLOWtargets an inactive relationship. - Every
UNFOLLOWtargets an active relationship. - Every
IS_FOLLOWINGreferences a snapshot that has already been created. - Commands contain single spaces between tokens.
- At least one command produces output.
More OpenAI problems
- Memory AllocatorPHONE SCREEN · Seen Jul 2026
- Message Event AggregationONSITE INTERVIEW · Seen Jul 2026
- Plant Infection Simulation, Part 4: Death CountdownPHONE SCREEN · Seen Jun 2026
- Streaming Entropy, Part 1: Batch EntropyONSITE INTERVIEW · Seen Jun 2026
- Streaming Entropy, Part 2: Numerically Stable EntropyONSITE INTERVIEW · Seen Jun 2026
- Streaming Entropy, Part 3: Block-wise EntropyONSITE INTERVIEW · Seen Jun 2026
- Streaming Entropy, Part 4: Stable Streaming EntropyONSITE INTERVIEW · Seen Jun 2026
- Resumable List IteratorPHONE SCREEN · Seen Jun 2026