Community Problem
Find Center of Star Graph
Difficulty: Easy
The first two edges of a star graph share exactly one endpoint, and that shared endpoint is the center.
Find Center of Star Graph
The first two edges of a star graph share exactly one endpoint, and that shared endpoint is the center.
By @fatimapark
January 5, 2026
·
Updated May 18, 2026
1,139 views
22
4.1 (9)
I had this on a phone screen and watched the candidate jump straight to building an adjacency map and counting degrees. That works in O(n), but it misses the structural shortcut: in a star graph, the center is the vertex shared by EVERY edge. So just look at the first two edges. The four endpoints contain the center exactly twice; pick the duplicate. The catalog covers Graph Representation but skipped this constant-time observation.
Find Center of Star Graph
There is an undirected star graph consisting of n nodes labeled from 1 to n. A star graph is a graph where there is one center node and exactly n - 1 edges that connect the center node with every other node.
You are given a 2D integer array edges where each edges[i] = [u_i, v_i] indicates that there is an edge between the nodes u_i and v_i. Return the center of the given star graph.
Examples
Example 1:
- Input:
edges = [[1, 2], [2, 3], [4, 2]] - Output:
2 - Explanation: Every edge involves vertex 2.
Example 2:
- Input:
edges = [[1, 2], [5, 1], [1, 3], [1, 4]] - Output:
1 - Explanation: Every edge involves vertex 1.
Example 3:
- Input:
edges = [[1, 2], [1, 3]] - Output:
1 - Explanation: First two edges are
[1, 2]and[1, 3]. Their shared endpoint is1, the center.
Example 4:
- Input:
edges = [[7, 1], [1, 8]] - Output:
1 - Explanation: Even with arbitrary labels, the shared endpoint of the first two edges is the center.
Constraints
3 <= n <= 10^5.edges.length == n - 1.edges[i].length == 2.1 <= u_i, v_i <= n.u_i != v_i.- The given
edgesrepresent a valid star graph.
Follow-up
Why does looking at only the first two edges suffice? In a star graph, the center is incident to ALL edges, including the first two. The non-center endpoints of edges 0 and 1 are different leaves (a leaf appears in exactly one edge). So the center is the unique vertex appearing in both edges[0] and edges[1].
Solution
Hints
Solution Walkthrough
Approach: Compare First Two Edges (O(1) time, O(1) space)
In a star graph, every edge has the center as one endpoint and a leaf as the other. The center appears in ALL n - 1 edges; each leaf appears in exactly one. So the center is the unique label that shows up in BOTH edges[0] and edges[1]. Comparing the four endpoints in constant time pinpoints it.
Algorithm
- Let
[a, b] = edges[0]and[c, d] = edges[1]. - If
a == cora == d, returna. - Otherwise return
b(the only other option).
Why It Works
The input is guaranteed to be a valid star graph, so the center exists and is unique. The first edge's two endpoints are (center, leaf_1). The second edge's two endpoints are (center, leaf_2). The four labels {a, b, c, d} partition into {center} (count 2) and {leaf_1, leaf_2} (count 1 each). Hence a is either the center or leaf_1. If a matches either c or d, it must be the center. Otherwise b is the center (and a is leaf_1).
Complexity
Metric Value
Time O(1)
Space O(1)Pitfalls
- Building an adjacency list and counting degrees. Correct but
O(n)and unnecessarily verbose. TheO(1)answer is right there in the structure. - Assuming the center is at index 0. Some interview write-ups say "the first endpoint is the center" but that is not part of the problem. The check must compare to BOTH endpoints of the second edge.
- Treating the graph as directed. Star graphs are undirected. The endpoint order in each edge is arbitrary; the comparison must allow either order.
Solution Code
