Decide whether adding at most two edges can make every node's degree even

Quick Overview

Decide whether adding at most two new edges, with no self-loops or repeated edges, can give every node of an undirected graph an even degree. It tests reasoning about degree parity, careful case analysis, and quick adjacency checks on large sparse graphs.

Decide whether adding at most two edges can make every node's degree even

Company: Hudson River Trading

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

There is an undirected graph consisting of `n` nodes numbered from `1` to `n`. You are given the integer `n` and a 2D array `edges` where `edges[i] = [a_i, b_i]` indicates that there is an edge between nodes `a_i` and `b_i`. The graph can be disconnected. You can add at most two additional edges (possibly none) to this graph, as long as there are no repeated edges and no self-loops. Return `True` if it is possible to make the degree of each node in the graph even, otherwise return `False`. The degree of a node is the number of edges connected to it. ### Function Signature ```python def is_possible(n: int, edges: list[list[int]]) -> bool: ``` ### Rules - Each pair `[a, b]` in `edges` is one undirected edge, so `[a, b]` and `[b, a]` would be the same edge. The input never repeats an edge. - A new edge must join two different nodes that are not already joined by an edge. When you add two edges, they must be different from each other. - A node that appears in no edge still exists and has degree 0. ### Constraints - `3 <= n <= 10^5` - `2 <= len(edges) <= 10^5` - `len(edges[i]) == 2` - `1 <= a_i, b_i <= n` - `a_i != b_i` - There are no repeated edges. ### Examples ```text Input: n = 5, edges = [[1, 2], [2, 3], [3, 4], [4, 2], [1, 4], [2, 5]] Output: True ``` The degrees of nodes 1 to 5 are 2, 4, 2, 3 and 1, so only nodes 4 and 5 are odd. They are not adjacent, and adding the edge (4, 5) makes every degree even. ```text Input: n = 4, edges = [[1, 2], [3, 4]] Output: True ``` All four nodes have degree 1. Adding (1, 2) and (3, 4) is not allowed because both edges already exist, but adding (1, 3) and (2, 4) works. ```text Input: n = 4, edges = [[1, 2], [1, 3], [1, 4]] Output: False ``` Node 1 has degree 3 and nodes 2, 3 and 4 have degree 1, so all four are odd. Two new edges that fix them would have to join node 1 to one of the others, and node 1 is already joined to all of them.

Overview: Decide whether adding at most two new edges, with no self-loops or repeated edges, can give every node of an undirected graph an even degree. It tests reasoning about degree parity, careful case analysis, and quick adjacency checks on large sparse graphs.

Hudson River Trading logo
Hudson River Trading
Nov 25, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

There is an undirected graph consisting of n nodes numbered from 1 to n. You are given the integer n and a 2D array edges where edges[i] = [a_i, b_i] indicates that there is an edge between nodes a_i and b_i. The graph can be disconnected.

You can add at most two additional edges (possibly none) to this graph, as long as there are no repeated edges and no self-loops.

Return True if it is possible to make the degree of each node in the graph even, otherwise return False.

The degree of a node is the number of edges connected to it.

Function Signature

def is_possible(n: int, edges: list[list[int]]) -> bool:

Rules

  • Each pair [a, b] in edges is one undirected edge, so [a, b] and [b, a] would be the same edge. The input never repeats an edge.
  • A new edge must join two different nodes that are not already joined by an edge. When you add two edges, they must be different from each other.
  • A node that appears in no edge still exists and has degree 0.

Constraints

  • 3 <= n <= 10^5
  • 2 <= len(edges) <= 10^5
  • len(edges[i]) == 2
  • 1 <= a_i, b_i <= n
  • a_i != b_i
  • There are no repeated edges.

Examples

Input: n = 5, edges = [[1, 2], [2, 3], [3, 4], [4, 2], [1, 4], [2, 5]]
Output: True

The degrees of nodes 1 to 5 are 2, 4, 2, 3 and 1, so only nodes 4 and 5 are odd. They are not adjacent, and adding the edge (4, 5) makes every degree even.

Input: n = 4, edges = [[1, 2], [3, 4]]
Output: True

All four nodes have degree 1. Adding (1, 2) and (3, 4) is not allowed because both edges already exist, but adding (1, 3) and (2, 4) works.

Input: n = 4, edges = [[1, 2], [1, 3], [1, 4]]
Output: False

Node 1 has degree 3 and nodes 2, 3 and 4 have degree 1, so all four are odd. Two new edges that fix them would have to join node 1 to one of the others, and node 1 is already joined to all of them.