Versioned Followers and Followees
Updated
Problem statement
A directed social graph has n users numbered from 0 to n - 1. A relationship u -> v means that user u follows user v.
Process a finite sequence of relationship updates, immutable snapshots, and historical list queries. Every snapshot includes all earlier updates. Later updates never change an earlier snapshot.
Operations
FOLLOW u vactivatesu -> v. The relationship is inactive immediately before the command.UNFOLLOW u vdeactivatesu -> v. The relationship is active immediately before the command.SNAPSHOTcreates the next snapshot. IDs start at0. Append the ID as a decimal string.GET_FOLLOWERS user snapshot_idappends all users who followeduserat that snapshot.GET_FOLLOWEES user snapshot_idappends all users whomuserfollowed at that snapshot.
Serialize each relationship list as a string containing ascending user IDs inside brackets, separated by commas and no spaces. Serialize an empty list as []. Only SNAPSHOT, GET_FOLLOWERS, and GET_FOLLOWEES produce output. Return those strings in command order.
Historical Storage
For this exercise, assume each directed relationship is stored as a change history and the whole graph is not copied for each snapshot. Use that history model to answer snapshot queries.
Function
getVersionedRelationships(n: int, operations: String[]) → String[]Examples
Example 1
n = 4operations = ["FOLLOW 0 1","FOLLOW 2 1","SNAPSHOT","GET_FOLLOWERS 1 0","GET_FOLLOWEES 0 0"]return = ["0","[0,2]","[1]"]At snapshot 0, users 0 and 2 follow user 1. User 0 follows only user 1.
Example 2
n = 3operations = ["SNAPSHOT","GET_FOLLOWERS 1 0","FOLLOW 0 1","FOLLOW 0 2","SNAPSHOT","GET_FOLLOWEES 0 0","GET_FOLLOWEES 0 1"]return = ["0","[]","1","[]","[1,2]"]Snapshot 0 is empty. The two later follows appear in snapshot 1 without changing snapshot 0.
Example 3
n = 5operations = ["FOLLOW 0 2","SNAPSHOT","FOLLOW 1 2","UNFOLLOW 0 2","FOLLOW 0 3","SNAPSHOT","GET_FOLLOWERS 2 0","GET_FOLLOWERS 2 1","GET_FOLLOWEES 0 1"]return = ["0","1","[0]","[1]","[3]"]User 0 follows 2 in snapshot 0. Before snapshot 1, user 1 follows 2, while user 0 replaces that relationship with 0 -> 3.
Constraints
1 <= n <= 20001 <= operations.length <= 200000 <= u, v, user < nandu != v- Every
FOLLOWtargets an inactive relationship. - Every
UNFOLLOWtargets an active relationship. - Every list query references a snapshot that has already been created.
- Commands contain single spaces between tokens.
- At least one command produces output.
More OpenAI Phone Screen questions
- Secret Number Search with One-Call-Delayed FeedbackPHONE SCREEN · Seen Nov 2026
- Determine the Winner of Three-Card HandsPHONE SCREEN · Seen Nov 2026
- Grid Infection with Recovery After D DaysPHONE SCREEN · Seen Nov 2026
- Sparse Plant Infection SimulationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Light Source Hits: Sample, Compare, and FitPHONE SCREEN · Seen Nov 2026
- Rectangle Point Coverage CountsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Adaptive Calibration ErrorPHONE SCREEN · Seen Nov 2026
- Elevator Scheduling SimulationPHONE SCREEN · Seen Nov 2026
- Evaluate Noisy AnnotatorsPHONE SCREEN · Seen Oct 2026
- Network Endpoint or Cycle BoundaryOA · PHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Consistent-Hash Packet RouterAmazon · PHONE SCREEN
- Snapshot Map with Sparse Version HistorySnorkel AI · PHONE SCREEN
- Task Scheduler with Prerequisites and Execution PathsBrex · PHONE SCREEN
- Top K Pins with Insert and QueryPinterest · PHONE SCREENPinterest interview questions
- Corporation Reporting LinesMicrosoft · PHONE SCREENMicrosoft interview questions
- Maintain a Consistent-Hashing RingApple · PHONE SCREENApple interview questions
- Time-Based Key-Value StoreOracle · PHONE SCREEN · ONSITE INTERVIEWOracle interview questions
- Time-Based Key-Value StoreCockroach Labs · PHONE SCREEN · ONSITE INTERVIEW
- Time Map with Sum and AverageConfluent · PHONE SCREENConfluent interview questions
- Time-Based Key-Value StoreVerkada · PHONE SCREENVerkada interview questions