Dynamic Friendship Connectivity
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks most first attempts at this Google OA question, reported in October 2026, is treating edge removal like it's free. You've got n servers, an empty multigraph, and a stream of operations: add a connection, remove one, ask if two servers are connected. Plain union-find handles the adds and the queries. It can't handle the removes. That gap is the whole problem. If you blank on how to get around it, StealthCoder runs invisibly on your screen during the live OA and gives you a working solution. Better to know the trick before you open the assessment.
The problem
There are n servers numbered from 0 through n - 1. Their undirected connection multigraph is initially empty. Process operations in order:
Examples
Example 1
n = 4
operations = [[0,0,1],[0,1,2],[2,0,2],[1,1,2],[2,0,2]]
return = [true,false]
The first two connections link servers 0 and 2. Removing {1,2} breaks that path before the second query.Reported by candidates. Source: FastPrep
Pattern and pitfall
Operation codes in the example: 0 adds an edge, 1 removes one, 2 queries connectivity. The multigraph part matters. Two servers can share several parallel edges, so removing {1,2} once doesn't always disconnect them. Track an edge count per pair in a hash map. The pitfall is plain union-find, which can't undo a union. Two real options. Offline: read all operations, record each edge's lifetime as a time interval, build a segment tree over time, and use union-find with rollback (union by size, no path compression) in a DFS over the tree. Or, if the removals are structured like a stack, rollback alone works. Offline dynamic connectivity runs in O((n + q) log q log n). Don't path-compress. It breaks rollback. StealthCoder is your hedge if the segment-tree-over-time idea won't come to you live. Know the shape before you sit down.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Dynamic Friendship Connectivity 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Google's OA.
Google reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Dynamic Friendship Connectivity FAQ
What's the trick in Dynamic Friendship Connectivity?+
Plain union-find can't delete edges. The standard fix is offline: collect every edge's active time interval, place those intervals into a segment tree over operation time, then DFS the tree applying unions and rolling them back on exit. Queries get answered at the leaves.
Why does the multigraph detail matter?+
Parallel edges between the same pair can exist, so one removal may leave the pair still connected. Keep a count per normalized pair (min, max) in a hash map. Only treat the edge as gone when its count hits zero.
Why can't I use path compression here?+
Path compression rewrites parent pointers in ways that are hard to undo. Rollback needs each union to be reversible, so use union by size or rank only, and push each change onto a stack. Find then costs O(log n), which is fine.
How hard is this really for a Google OA?+
Hard. It's a known offline dynamic connectivity problem, and most candidates stall on deletions. If you've seen union-find with rollback, it's manageable. If not, expect to spend most of your time figuring out the time-interval segment tree idea.
How do I prepare in 48 hours?+
Write union-find with rollback from scratch once. Then write the segment-tree-over-time DFS on a small case. Trace the example by hand: add 0-1, add 1-2, query, remove 1-2, query. Check that your answers come out true then false.