Remove the Last Input Edge on the Unique Cycle

Quick Overview

Remove the Last Input Edge on the Unique Cycle. edges describes a connected undirected graph formed by adding one previously absent edge to a tree on vertices 1..n.

Remove the Last Input Edge on the Unique Cycle

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem edges describes a connected undirected graph formed by adding one previously absent edge to a tree on vertices 1..n. Return an edge whose removal restores a tree. If several edges work, return the one occurring last in the input array. ```cpp class Solution { public: vector<int> findRedundantConnection(vector<vector<int>>& edges); }; ``` ### Examples **Example 1:** ```text edges = [[1,2],[2,3],[3,4],[1,4]] Output: [1,4] Explanation: It is the last listed edge on the cycle. ``` **Example 2:** ```text edges = [[1,2],[2,3],[1,3],[3,4]] Output: [1,3] Explanation: The later [3,4] is a bridge, so it is not removable. ``` ### Constraints - There are n edges and n vertices, with n in 3..1000. Each edge has two different labels `a < b` in 1..n. Edges are not repeated, and the whole graph is connected with the tree-plus-one-edge promise. ### Clarifying Questions Use undirected connectivity and preserve the last-in-input rule among cycle edges. ```hint Read the boundary condition Which processed edge first joins vertices that already have a path between them? ``` ### Contract considerations Use undirected connectivity and preserve the last-in-input rule among cycle edges.

Overview: Remove the Last Input Edge on the Unique Cycle. edges describes a connected undirected graph formed by adding one previously absent edge to a tree on vertices 1..n.

Google logo
Google
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

edges describes a connected undirected graph formed by adding one previously absent edge to a tree on vertices 1..n. Return an edge whose removal restores a tree. If several edges work, return the one occurring last in the input array.

class Solution {
public:
    vector<int> findRedundantConnection(vector<vector<int>>& edges);
};

Examples

Example 1:

edges = [[1,2],[2,3],[3,4],[1,4]]
Output: [1,4]
Explanation: It is the last listed edge on the cycle.

Example 2:

edges = [[1,2],[2,3],[1,3],[3,4]]
Output: [1,3]
Explanation: The later [3,4] is a bridge, so it is not removable.

Constraints

  • There are n edges and n vertices, with n in 3..1000. Each edge has two different labels a < b in 1..n. Edges are not repeated, and the whole graph is connected with the tree-plus-one-edge promise.

Clarifying Questions Guidance

Use undirected connectivity and preserve the last-in-input rule among cycle edges.

Contract considerations

Use undirected connectivity and preserve the last-in-input rule among cycle edges.