Find All People with the Secret
Problem statement
There are n people labeled from 0 to n - 1. At time 0, person 0 shares a secret with firstPerson.
Each row [x, y, time] in meetings describes a meeting between people x and y. If either knows the secret at that time, the other learns it. Sharing is instantaneous: the secret can travel through a chain of meetings at the same time, regardless of their input order.
Examples
Example 1
n = 6meetings = [[2,3,5],[1,2,5],[4,5,6]]firstPerson = 1return = [0,1,2,3]At time 5, person 1 shares through person 2 to person 3, even though the second link is listed first. Neither participant at time 6 knows the secret.
Unlock this recently reported problem
FastPrep Pro gives you full access to interview problems reported within the last week.
- Full problem statement and constraints
- 2 more worked examples, explained
- Guided hints and editorial
- Run your code on real test cases
$99 billed yearly — or $19 month-to-month. Cancel anytime.