Find Earliest Full Connectivity Timestamp
Company: Uber
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of graph connectivity and temporal event processing, examining how pairwise interactions over time can merge components into a single connected set.
Part 1: Earliest Timestamp of Full Connectivity
Constraints
- 0 <= len(users) <= 100000
- All user IDs in users are unique
- 0 <= len(logs) <= 200000
- Each timestamp is a non-negative integer
- Every log references users from the given users list
Examples
Input: (['A', 'B', 'C', 'D'], ['100 A B', '120 C D', '150 B C'])
Expected Output:
Explanation: The first two logs create two groups, and the third log merges them into one.
Input: (['A', 'B', 'C'], ['5 A B'])
Expected Output:
Explanation: User C never becomes connected to the others.
Input: (['A'], [])
Expected Output:
Explanation: A single user is already fully connected.
Input: (['A', 'B'], ['7 A B', '8 A B'])
Expected Output:
Explanation: The users become fully connected at the first log.
Hints
- A disjoint set union structure can merge groups efficiently as each log arrives.
- Track how many connected components remain so you can stop as soon as it becomes 1.
Part 2: Earliest Full Connectivity with Multiple Event Types
Constraints
- 0 <= len(users) <= 100000
- All user IDs in users are unique
- 0 <= len(logs) <= 200000
- Each event type is a single token with no spaces
- Every log references users from the given users list
Examples
Input: (['A', 'B', 'C', 'D'], ['100 A B shared_ride', '110 A C message', '120 C D shared_ride', '150 B C shared_ride'], ['shared_ride'])
Expected Output:
Explanation: The message event is ignored, so all users connect only at timestamp 150.
Input: (['A', 'B', 'C'], ['5 A B follow', '7 B C shared_ride', '9 A C carpool_bonus'], ['shared_ride', 'carpool_bonus'])
Expected Output:
Explanation: The follow event is ignored. The shared_ride and carpool_bonus events fully connect the graph by 9.
Input: (['A', 'B'], ['10 A B message'], ['shared_ride'])
Expected Output:
Explanation: No allowed event type appears.
Input: ([], [], ['shared_ride'])
Expected Output:
Explanation: An empty user list is considered already fully connected.
Hints
- Convert the allowed event types to a set so membership checks are fast.
- Ignore logs with irrelevant event types and only union users for relevant ones.
Part 3: Earliest Full Connectivity from Unsorted Logs
Constraints
- 0 <= len(users) <= 100000
- All user IDs in users are unique
- 0 <= len(logs) <= 200000
- Each timestamp is a non-negative integer
- Every log references users from the given users list
Examples
Input: (['A', 'B', 'C', 'D'], ['150 B C', '100 A B', '120 C D'])
Expected Output:
Explanation: Sorting by timestamp gives 100, 120, 150, and full connectivity first appears at 150.
Input: (['A', 'B', 'C'], ['20 A B', '10 B C'])
Expected Output:
Explanation: After sorting, B and C connect at 10, then A joins at 20.
Input: (['A', 'B', 'C'], ['5 B C', '5 A B'])
Expected Output:
Explanation: Both logs have the same timestamp, so the network becomes fully connected at time 5.
Input: (['A', 'B'], [])
Expected Output:
Explanation: With no logs, two users never become connected.
Hints
- If logs are not sorted, think about what must happen before a one-pass connectivity algorithm becomes valid.
- After sorting by timestamp, the same disjoint set union approach from the basic problem still works.
Part 4: Report the Complexity of the Connectivity Approach
Constraints
- 0 <= num_users <= 10^18
- 0 <= num_logs <= 10^18
- logs_sorted is either True or False
Examples
Input: (4, 3, True)
Expected Output:
Explanation: Sorted logs need only the union-find pass.
Input: (4, 3, False)
Expected Output:
Explanation: Unsorted logs require sorting before union-find processing.
Input: (1, 0, True)
Expected Output:
Explanation: The reported formula depends on the algorithmic setup, not on the specific small values.
Input: (100000, 0, False)
Expected Output:
Explanation: If sorting is required in the plan, report the unsorted-log complexity form.
Hints
- Union-find handles each merge/find in amortized near-constant time, summarized as alpha(n).
- When logs are unsorted, sorting dominates the extra work and usually requires storing the logs.
Part 5: Earliest Full Connectivity in a Log Stream
Constraints
- 0 <= len(users) <= 100000
- The log stream can be much larger than memory, so the intended solution should not store all logs
- Log timestamps are already in nondecreasing order
- Every log references users from the given users list
Examples
Input: (['A', 'B', 'C', 'D'], ['100 A B', '120 C D', '150 B C'])
Expected Output:
Explanation: A single pass over the ordered stream is enough to detect the first moment of full connectivity.
Input: (['A', 'B', 'C'], ['1 A B', '2 A B', '3 B C', '4 A C'])
Expected Output:
Explanation: The duplicate connection at 2 changes nothing, and the graph becomes fully connected at 3.
Input: (['A', 'B', 'C'], ['1 A B'])
Expected Output:
Explanation: The stream ends before all users join one component.
Input: ([], [])
Expected Output:
Explanation: No users means the system is trivially fully connected.
Hints
- Because the stream is already time-ordered, you can update connectivity as each event arrives and stop immediately once one component remains.
- A disjoint set union structure lets you keep only component information instead of the full history.