The Earliest Moment When Everyone Become Friends
A medium-tier problem at 66% community acceptance, tagged with Array, Union Find, Sorting. Reported in interviews at Expedia and 1 others.
You're looking at a union-find problem dressed up as a social narrative. Expedia and Google both ask it. The premise is deceptively simple: you're given timestamps when pairs become friends, and you need to find the exact moment everyone in a group is connected. The trap is thinking you need to simulate the whole timeline. You don't. Sort the friendships, build a union-find structure, and track when the last critical connection closes the loop. Miss the sorting step or confuse when to check connectivity, and you'll time out or return the wrong answer under pressure.
Companies that ask "The Earliest Moment When Everyone Become Friends"
The Earliest Moment When Everyone Become Friends is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by an engineer at a top-10 tech company who can solve these problems cold but didn't want to trust himself in a 90-minute screen share.
Get StealthCoderThe algorithm hinges on recognizing that you don't validate friendship transitivity in real time. Instead, sort all friendships by timestamp, process them in order through a union-find data structure, and check after each merge whether all people belong to a single connected component. The gotcha: candidates often iterate through timestamps and check friendship status at each one, which is quadratic and fails on large inputs. The real insight is that the answer exists at a specific friendship event, not some arbitrary moment in between. Union-find gives you near-constant amortized lookups. If you hit this problem cold in an assessment and aren't fluent in union-find path compression, StealthCoder surfaces the pattern instantly so you code it correctly without getting stuck on the data structure implementation.
Pattern tags
You know the problem.
Make sure you actually pass it.
The Earliest Moment When Everyone Become Friends recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by an engineer at a top-10 tech company who can solve these problems cold but didn't want to trust himself in a 90-minute screen share. Works on HackerRank, CodeSignal, CoderPad, and Karat.
The Earliest Moment When Everyone Become Friends interview FAQ
Is this really asked at Google and Expedia?+
Yes. Both companies have reported asking it. It shows up at mid-level and senior rounds. The acceptance rate is 65.7%, so it's not trivial, but it's not in the top 5% hardest either. If you get it in an OA, you're expected to code it clean.
What's the trick I'm missing if my brute force times out?+
You're likely checking connectivity after every timestamp, not just after friendships. Sort the friendships, apply union-find with path compression, and check for a single connected component only when you process an edge. Naive O(n^2) checks fail on large n.
How does union-find apply here?+
Each person is a node. Each friendship is an edge. Union-find tracks connected components efficiently. When you union two people, you merge their components. When all n people are in one component, everyone's connected. Use rank or path compression to keep operations near O(1).
Do I need to handle edge cases like duplicate friendships?+
Yes. If two people are already in the same component and you try to union them again, nothing changes. Your union operation should handle that without inflating the component count or the result timestamp.
Can I solve this without union-find?+
Technically, yes, with DFS or BFS on the friendship graph. But that's slower and messier under time pressure. Union-find is the intended pattern. If you're not comfortable with it, this is a problem where StealthCoder gives you a working template in seconds so you don't derail your assessment.
Want the actual problem statement? View "The Earliest Moment When Everyone Become Friends" on LeetCode →