Find every connection whose removal disconnects a server network

Quick Overview

Given a connected network of servers and their undirected connections, return every connection whose removal would split the network into separate components. The problem tests depth-first search on graphs, discovery and low-link values for finding bridges, and an iterative traversal that handles up to 100,000 servers.

Find every connection whose removal disconnects a server network

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

There are `n` servers numbered from `0` to `n - 1`, joined by undirected connections that form a network. Each `connections[i] = [a, b]` means servers `a` and `b` are directly connected. Every server can reach every other server, directly or through other servers. A connection is critical if removing it would split the network into two or more disconnected components. Find all critical connections. ### Function Signature ```python def critical_connections(n: int, connections: list[list[int]]) -> list[list[int]]: ``` ### Rules - Connections are undirected: `[a, b]` and `[b, a]` describe the same connection. - Write each critical connection as `[u, v]` with `u < v`, whatever order its endpoints have in the input. - Return the critical connections sorted in ascending order, by `u` and then by `v`. - If no connection is critical, return an empty list. ### Constraints - `2 <= n <= 10^5` - `n - 1 <= connections.length <= 10^5` - `0 <= a, b < n` for every connection `[a, b]` - `a != b` (no server is connected to itself) - No repeated connections - The network is connected ### Examples ```text Input: n = 4, connections = [[0,1],[1,2],[2,0],[1,3]] Output: [[1,3]] ``` Servers 0, 1 and 2 form a cycle, so removing any one of those three connections leaves them connected. Server 3 is reachable only through `[1,3]`, so that connection is the only critical one. ```text Input: n = 5, connections = [[0,1],[1,2],[2,3],[3,4]] Output: [[0,1],[1,2],[2,3],[3,4]] ``` The network is a single path with no cycles, so removing any connection splits it in two. All four connections are critical and appear in ascending order. ```text Input: n = 7, connections = [[0,1],[1,2],[2,0],[3,2],[3,4],[4,5],[5,3],[6,0]] Output: [[0,6],[2,3]] ``` The triangles {0, 1, 2} and {3, 4, 5} are joined only by `[3,2]`, and server 6 hangs off server 0 through `[6,0]`. Each of those two connections is critical. They are written with the smaller server first and then sorted. Every other connection lies on a triangle.

Overview: Given a connected network of servers and their undirected connections, return every connection whose removal would split the network into separate components. The problem tests depth-first search on graphs, discovery and low-link values for finding bridges, and an iterative traversal that handles up to 100,000 servers.

Nov 24, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

There are n servers numbered from 0 to n - 1, joined by undirected connections that form a network. Each connections[i] = [a, b] means servers a and b are directly connected. Every server can reach every other server, directly or through other servers.

A connection is critical if removing it would split the network into two or more disconnected components. Find all critical connections.

Function Signature

def critical_connections(n: int, connections: list[list[int]]) -> list[list[int]]:

Rules

  • Connections are undirected: [a, b] and [b, a] describe the same connection.
  • Write each critical connection as [u, v] with u < v , whatever order its endpoints have in the input.
  • Return the critical connections sorted in ascending order, by u and then by v .
  • If no connection is critical, return an empty list.

Constraints

  • 2 <= n <= 10^5
  • n - 1 <= connections.length <= 10^5
  • 0 <= a, b < n for every connection [a, b]
  • a != b (no server is connected to itself)
  • No repeated connections
  • The network is connected

Examples

Input: n = 4, connections = [[0,1],[1,2],[2,0],[1,3]]
Output: [[1,3]]

Servers 0, 1 and 2 form a cycle, so removing any one of those three connections leaves them connected. Server 3 is reachable only through [1,3], so that connection is the only critical one.

Input: n = 5, connections = [[0,1],[1,2],[2,3],[3,4]]
Output: [[0,1],[1,2],[2,3],[3,4]]

The network is a single path with no cycles, so removing any connection splits it in two. All four connections are critical and appear in ascending order.

Input: n = 7, connections = [[0,1],[1,2],[2,0],[3,2],[3,4],[4,5],[5,3],[6,0]]
Output: [[0,6],[2,3]]

The triangles {0, 1, 2} and {3, 4, 5} are joined only by [3,2], and server 6 hangs off server 0 through [6,0]. Each of those two connections is critical. They are written with the smaller server first and then sorted. Every other connection lies on a triangle.