Dynamic Friendship Connectivity
Updated
Problem statement
There are n servers numbered from 0 through n - 1. Initially, there are no connections. Connections work in both directions, and there may be more than one connection between the same two servers. Process operations in order:
[0, u, v]adds one connection betweenuandv. Parallel connections are allowed.[1, u, v]removes one existing connection betweenuandv. Other parallel connections remain active.[2, u, v]asks whetheruandvare connected by a path along which at least one connection remains between each neighboring pair.
Return the answers to all query operations in their original order. A server is connected to itself, so every query with u == v returns true.
Function
dynamicFriendshipConnectivity(n: int, operations: int[][]) → boolean[]Examples
Example 1
n = 4operations = [[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.
Example 2
n = 3operations = [[0,0,1],[0,1,0],[2,0,1],[1,0,1],[2,0,1],[1,1,0],[2,0,1],[2,2,2]]return = [true,true,false,true]Two parallel connections join 0 and 1. Removing one occurrence leaves the other active; removing the second disconnects them. Server 2 is always connected to itself.
Example 3
n = 4operations = [[0,0,1],[0,1,2],[0,0,2],[2,0,2],[1,0,2],[2,0,2],[1,1,2],[2,0,2]]return = [true,true,false]After the direct edge {0,2} is removed, the path through server 1 still connects the endpoints. Removing {1,2} then breaks the last path.
Constraints
1 <= n <= 100000.1 <= operations.length <= 100000, and at least one operation is a query.operations[i].length == 3.operations[i][0]is0,1, or2.0 <= operations[i][1], operations[i][2] < n.- Addition and removal endpoints are distinct.
- Every removal has at least one matching active connection occurrence.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Count Islands from Streaming RowsAmazon · ONSITE INTERVIEW
- Find All People With SecretIDFC Bank · ONSITE INTERVIEWIDFC Bank interview questions
- Maximum Edge Removals for Two TravelersMorgan Stanley · ONSITE INTERVIEWMorgan Stanley interview questions
- Making a Large IslandMeta · ONSITE INTERVIEWMeta interview questions
- Largest Island After One Land ConversionBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Water Supply With Wells and PipesNutanix · ONSITE INTERVIEWNutanix interview questions
- Evaluate DivisionPlaud · ONSITE INTERVIEW
- Count Friend CirclesOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Radar Barrier CrossingSnap Inc. · ONSITE INTERVIEWSnap Inc. interview questions
- Accounts MergeOracle · ONSITE INTERVIEWOracle interview questions