Earliest Full Connectivity with Edge Removals
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Google flagged this one in September 2026, and the constraints are the tell. With n and the log count both capped at 2000, the first instinct is to rebuild the graph and check connectivity after every timestamp group. That works. It's about 2000 groups times an O(n + m) traversal, so roughly 8 million operations. The real trap is the atomic grouping and the edge removals, which kill the plain union-find shortcut. If you blank on the OA, StealthCoder runs invisibly as a safety net, but the logic here is small enough to own yourself.
The problem
Process chronological logs [timestamp, operation, u, v] for an undirected social graph. Operation is ADD or REMOVE. All logs with the same timestamp take effect atomically. Return the earliest timestamp after whose complete group all n users are connected. Duplicate additions and absent-edge removals are no-ops. Return -1 if this never occurs, and 0 for at most one user. Function firstFullyConnectedTimestamp(n: int, logs: String[][]) → long Examples Example 1 n = 4 logs = [["1","ADD","0","1"],["2","ADD","2","3"],["3","ADD","1","2"]] return = 3 All four users first connect at timestamp 3. Example 2 n = 3 logs = [["1","ADD","0","1"],["2","ADD","1","2"],["2","REMOVE","0","1"]] return = -1 The atomic timestamp-2 state is disconnected. Example 3 n = 1 logs = [] return = 0 One user is connected without an edge. Constraints 0 <= n <= 2000. 0 <= logs.length <= 2000. Logs are sorted by nondecreasing timestamp.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The pattern is graph connectivity with dynamic edges. Union-find can't undo unions, so don't reach for it first. Keep an edge set (normalize each pair as min,max so duplicates and absent removals become no-ops) and process logs in groups of equal timestamp. Apply every ADD and REMOVE in the group, then and only then check connectivity with a BFS or DFS over the current edges. Return the group's timestamp the first time all n nodes are reached. Checking mid-group is the classic pitfall, and Example 2 exists to punish it. Also handle n <= 1 up front by returning 0, even with empty logs. Return -1 if no group ever works. The cost is fine at these limits, and a quick edge-count check (fewer than n-1 edges means disconnected) skips many traversals. If you freeze live, StealthCoder is the hedge for the OA, but this is a clean group, apply, check loop.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Earliest Full Connectivity with Edge Removals cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Google's OA.
Google reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Earliest Full Connectivity with Edge Removals FAQ
What's the trick in this Google OA problem?+
Treat each timestamp as one atomic batch. Apply every ADD and REMOVE in the group to an edge set, then run one connectivity check. Checking between operations in the same group gives wrong answers, which is exactly what Example 2 tests.
Why not just use union-find?+
Union-find only merges. It can't undo a union when an edge is removed. You could use offline dynamic connectivity, but at n and logs up to 2000, rebuilding with BFS or DFS per timestamp group is simpler and fast enough.
How hard is this really?+
Medium. The idea is simple, but the details bite. You need atomic grouping, normalized edges, no-op handling for duplicates, and the n <= 1 edge case that returns 0. Miss one and a hidden test fails.
What edge cases should I test before submitting?+
Test n = 0 and n = 1 with empty logs, which return 0. Test duplicate ADDs, REMOVE of a missing edge, and an ADD and REMOVE of the same edge in one timestamp. Also test a graph that connects and later disconnects, where the earliest time still counts.
How do I prepare in 48 hours?+
Write a BFS connectivity check from scratch, then the grouping loop over sorted logs. Practice normalizing undirected edges into a set. Run Example 2 by hand. Once those three pieces feel automatic, you're ready for this problem and its variants.