Ctrl K

Hub of the Network

Easy
A small network has n devices, each with its own number from 1 to 10⁵. It is wired as a star: one device is the hub, and every other device has a single cable running to it. There are n - 1 cables in total. You are given the list of cables links, where links[i] = [a, b] means devices a and b are connected. Cables, and the two ends of each cable, come in no particular order. Return the number of the hub.
Example 1
links=[[3, 1], [2, 3], [3, 4]] → 3

Node 3 appears in all three links, so it is the hub.

Example 2
links=[[5, 2], [1, 2], [2, 4], [3, 2]] → 2

Every link goes through node 2.

Constraints
  • 3 ≤ n ≤ 10⁵
  • len(links) == n - 1
  • links[i] = [a, b] with 1 ≤ a, b ≤ 10⁵ and a ≠ b
  • The links really form a star
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTrusted by All, Trusts None EasyWill the Message Get Through EasyModule Build Order MediumCourse Enrollment Queue MediumDo the Links Form a Tree Medium
loading Python…
Input
links=[[3, 1], [2, 3], [3, 4]]
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 50 tests on the server, hidden ones included.