Implement Disease and Friend Snapshot Models
OpenAISoftware Engineer · OnsiteLast updated
This question evaluates skills in graph-based modeling and temporal state simulation as well as the design and implementation of snapshot-capable directed graph data structures, covering competencies in propagation and state transitions (including immunity, recovery, and death), efficient snapshotting, querying, ranking, and diffing of follow relationships. It is commonly asked to assess algorithmic reasoning, correctness under edge cases, and complexity-aware implementation; the problem belongs to the Coding & Algorithms domain and tests both conceptual understanding of dynamic graph behavior and practical application-level implementation of data structures and immutable snapshots.
You are asked to solve two independent coding problems.
Problem 1: Simulate disease spread on a contact graph
There are n people labeled 0 through n - 1. An undirected edge between two people means the disease can spread between them. Time advances in whole days, and all state transitions for a day are based on the state at the start of that day unless otherwise stated.
Implement the following follow-ups:
-
daysToInfectAll(n, edges, initiallyInfected): Initially, every person is susceptible except the initially infected people. Each day, every infected person infects all susceptible neighbors. Return the minimum number of days until everyone is infected. Return-1if some people can never be infected. -
daysToFinishWithImmune(n, edges, initiallyInfected, initiallyImmune): Some people are initially immune. Immune people cannot be infected and do not transmit the disease. Return how many days it takes until no new infection can occur. Also be prepared to report which susceptible people remain uninfected. -
daysToStableAfterRecovery(n, edges, initiallyInfected, initiallyImmune, recoveryDays): An infected person remains contagious forrecoveryDaysdays after becoming infected, then becomes immune at the end of that day. Return the first day when the system is stable, meaning no infected people remain and no future infections are possible. -
Extend the model with a
Deadstate. The input provides either a per-person terminal outcome or a deterministic predicate such aswillDie(person), plus the number of days before the terminal transition. Dead people cannot be infected and cannot transmit. Return the day the system becomes stable and the final count of susceptible, infected, immune, and dead people.
Clarify edge cases such as an empty initial infection set, disconnected components, simultaneous infection and recovery, and whether recovery or death happens before or after that day’s transmission.
Problem 2: Implement a friend-circle snapshot data structure
Implement a class FriendCircle supporting directed follow relationships between users.
Required operations:
follow(userId, targetId):userIdfollowstargetId.unfollow(userId, targetId):userIdstops followingtargetIdif the relationship exists.snapshot(): Capture an immutable snapshot of the current follow graph and return a snapshot id.getFriends(snapshotId, userId): Given a snapshot id, return all users followed byuserIdat that snapshot.recommendFriends(snapshotId, userId, k): Given a snapshot id, recommend up tokusers thatuserIddoes not already follow. A reasonable ranking rule is to recommend two-hop candidates: ifuserIdfollowsaandafollowsx, thenxis a candidate. Rank candidates by the number of mutual intermediate users, then by user id or another deterministic tie-breaker.diffFriends(snapshotIdA, snapshotIdB, userId): Compare two snapshots and return the users added to and removed fromuserId’s follow list.
Be prepared to discuss API design, whether snapshot-specific logic belongs in FriendCircle or a separate Snapshot abstraction, time and space complexity, and how to avoid copying the entire graph on every snapshot.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.