Earliest Time All Fulfillment Centers Are Linked by Contracts
Last updated: November 29, 2026
Quick Overview
Fulfillment centers sign transfer contracts over time, each recorded as (timestamp, a, b), and connectivity is transitive. Return the earliest timestamp at which every center can reach every other, or -1 if that never happens.
Amazon
November 29, 20260
0
0 solved
Fulfillment centers sign transfer contracts over time, each recorded as (timestamp, a, b), and connectivity is transitive. Return the earliest timestamp at which every center can reach every other, or -1 if that never happens.
A quick Amazon phone screen variant of the "everyone becomes connected" pattern. The interviewer looks for sorting by time, tracking the number of components in a union find, and stopping as soon as it reaches one.
What the Interviewer Expects
- Sort events by timestamp
- Track the component count and decrement on successful unions
- Return the timestamp when the count hits 1
- Return -1 otherwise
- O(m log m) time
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- What if contracts can expire?
- How would you report the moment each center first joins the giant component?
- How would you handle millions of events arriving as a stream?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: n centers and events (t, a, b) in arbitrary order. Output: earliest t when the graph of contracts so far is connected, or -1. Clarify what to return when n is 1 (I return 0)....
High-Level Design
Connectivity only increases over time, so sort events by timestamp and replay them. Start with n components. Each union that merges two different roots reduces the count by one. The first time the cou...