2daysbeforeinterview
Home1Companies2Problems3Experiences4Compensation5Assistant

Spaces

Saved work

Your prep

Notes6Bookmarks7Submissions

Community

Leaderboard8Send feedback
Contribute9
2daysbeforeinterview
2daysbeforeinterview

Straight from the interview room.

Browse

  • Companies
  • Problems
  • Experiences
  • Compensation
  • Leaderboard
  • Pricing

Contribute

  • Add a question
  • Share an experience
  • Report compensation
  • Committed Contributor
  • Send feedback

About

  • About 2daysbeforeinterview
  • Contact
  • Privacy
  • Terms
  • Refunds
  • Delivery

© 2026 2daysbeforeinterview

  • Instagram(opens in a new tab)
  • YouTube(opens in a new tab)
  • X (Twitter)(opens in a new tab)
  • help@2daysbeforeinterview.com
HomeCompaniesProblems
Keep holding ⌥Alt and press a number · ? for every shortcut
Back to UberFunction: All functionsEngineeringDataMachine learningProductDesignQuality assuranceBusiness
Home1Companies2Problems3Experiences4Compensation5Assistant

Spaces

Saved work

Your prep

Notes6Bookmarks7Submissions

Community

Leaderboard8Send feedback
Back to Uber
DSA
1 reportlast asked …
Uber

Earliest Moment When Everyone Becomes Connected

You are given n entities labeled 0 to n - 1 and a list of connection logs logs. Each log is [timestamp, x, y] and means x and y became directly connected at timestamp. The logs are not necessarily in time order.

Connectivity is transitive: if a is connected to b and b is connected to c, then a is connected to c.

Return the earliest timestamp at which every entity is connected to every other, directly or transitively. If that never happens, return -1.

Example 1

Input:  n = 4, logs = [[20,0,1],[50,2,3],[30,1,2],[70,0,3],[60,1,3]]
Output: 50

In time order: at 20 entities 0 and 1 join, at 30 entity 2 joins them, and at 50 entity 3 joins too, so all four are connected. Reading the logs in the given order would wrongly stop at 30.

Example 2

Input:  n = 4, logs = [[3,0,1],[7,1,2],[9,0,2]]
Output: -1

Entity 3 never appears in any log, so it is never connected to the others.

Example 3

Input:  n = 2, logs = [[0,1,0]]
Output: 0

The only two entities connect at timestamp 0.

Constraints

  • 2 <= n <= 100
  • 1 <= logs.length <= 10^4
  • logs[i].length == 3
  • 0 <= logs[i][0] <= 10^9
  • 0 <= logs[i][1], logs[i][2] <= n - 1
  • logs[i][1] != logs[i][2]
  • All timestamps are unique.
  • Each pair of entities appears in at most one log.

Hints

0/3

Domains

Backend
asked Mar 2026Report
Discussion
Related questions
Asked atUber
My notes
Practice
EditorialLocked
Community solutions
Learning resources(3)
Uber
Uber