Find Center of Star Graph

EasyGraphshashing

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

An undirected **star graph** has n nodes labelled 1 to n: one center node is connected by an edge to every other node, and there are no other edges (so there are exactly n - 1 edges). You are given edges, where each edges[i] = [u, v] is an edge between u and v. The input is guaranteed to be a valid star graph. Return the label of the center. This can be done in O(1) time.

Examples

Input: edges = [[1,2],[2,3],[4,2]]
Output: 2
Node 2 appears in every edge.
Input: edges = [[1,2],[5,1],[1,3],[1,4]]
Output: 1
Node 1 is connected to all other nodes.

Constraints

  • 3 <= n <= 10^5
  • `edges.length == n - 1`
  • `edges[i].length == 2`, `1 <= u, v <= n`, `u != v`
  • `edges` represents a valid star graph.

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More hashing problems