Register for our webinar

### How to Nail your next Technical Interview

1 hour
1
Enter details
2
Select webinar slot
*Invalid Name
*Invalid Name
Step 1
Step 2
Congratulations!
You have registered for our webinar
Oops! Something went wrong while submitting the form.
1
Enter details
2
Select webinar slot
*All webinar slots are in the Asia/Kolkata timezone
Step 1
Step 2
Confirmed
You are scheduled with Interview Kickstart.
Redirecting...
Oops! Something went wrong while submitting the form.

## You may be missing out on a 66.5% salary hike*

### Nick Camilleri

Head of Career Skills Development & Coaching
*Based on past data of successful IK students
Help us know you better!

## How many years of coding experience do you have?

Oops! Something went wrong while submitting the form.

## FREE course on 'Sorting Algorithms' by Omkar Deshpande (Stanford PhD, Head of Curriculum, IK)

Oops! Something went wrong while submitting the form.

# Find All People With Secret Problem Statement

There are n people numbered from 0 to n - 1. A list of meetings is given whose each entry is a tuple [xi, yi, timei], indicating that the person xi and the person yi have a meeting at timei.
Person 0 has a secret and initially shares the secret with a given person at time 0. This secret is then shared every time a meeting takes place with the person that has the secret.
Return a list of all the people who have the secret after all the meetings have taken place.

## Example One

{
"n": 6,
"meetings": [
[1, 2, 3],
[2, 3, 5],
[1, 5, 2],
[3, 5, 10]
],
"first_person": 2
}

Output:

[0, 1, 2, 3, 5]

At time 0, person 0 shares the secret with person 2.
At time 2, neither person 1 nor person 5 knows the secret.
At time 3, person 2 shares the secret with person 1.
At time 5, person 2 shares the secret with person 3.
At time 10, person 3 shares the secret with person 5.
Thus, people 0, 1, 2, 3, and 5 know the secret after all the meetings.

## Example Two

{
"n": 5,
"meetings": [
[2, 3, 1],
[2, 4, 1],
[1, 2, 1]
],
"first_person": 1
}

Output:

[0, 1, 2, 3, 4]

At time 0, person 0 shares the secret with person 1.
At time 1, person 1 shares the secret with person 2, and person 2 shares the secret with persons 3 and 4.
Thus, people 0, 1, 2, 3, and 4 know the secret after all the meetings.

## Notes

• A person may attend multiple meetings at the same time.
• The secrets are shared instantaneously, which means a person may receive the secret and share it with people in other meetings within the same time frame.
• Return the output list in any order.

Constraints:

• 2 <= n <= 105
• 0 <= size of the input list <= 105
• 0 <= xi, yi <= n - 1
• xi != yi
• 1 <= timei <= 105
• 1 <= first person with whom secret is shared at time 0 <= n - 1

## Code For Find All People With Secret Solution: Bfs

/*
Asymptotic complexity in terms of the number of meetings \`m\` and the number of people \`n\`:
* Time: O(m * log(m) + n).
* Auxiliary space: O(n + m).
* Total space: O(n + m).
*/
vector<int> find_all_people_with_secret(int n, vector<vector<int>> &meetings, int first_person) {
vector<vector<pair<int,int>>> graph(n);
for (int i = 0; i < meetings.size(); i++) {
graph[meetings[i][0]].push_back({meetings[i][1], meetings[i][2]});
graph[meetings[i][1]].push_back({meetings[i][0], meetings[i][2]});
}

// Min Heap which stores a pair of elements where the first entry denotes the current
// time and the second entry denotes the person who knows the secret.
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
vector<int> is_secret_known(n, false);

pq.push({0, 0});
pq.push({0, first_person});

while (!pq.empty()) {
int current_time = pq.top().first;
int person_x = pq.top().second;
pq.pop();

if (is_secret_known[person_x]) {
continue;
}

is_secret_known[person_x] = true;

for (int i = 0; i < graph[person_x].size(); i++) {
int person_y = graph[person_x][i].first;
int meeting_time = graph[person_x][i].second;
if (is_secret_known[person_y]) {
continue;
}
if (meeting_time >= current_time) {
pq.push({meeting_time, person_y});
}
}
}

vector<int> people;
for (int i = 0; i < n; i++) {
if (is_secret_known[i]) {
people.push_back(i);
}
}

return people;
}

We hope that these solutions to the 4 sum problem have helped you level up your coding skills. You can expect problems like these at top tech companies like Amazon and Google.

If you are preparing for a tech interview at FAANG or any other Tier-1 tech company, register for Interview Kickstart’s FREE webinar to understand the best way to prepare.

Interview Kickstart offers interview preparation courses taught by FAANG+ tech leads and seasoned hiring managers. Our programs include a comprehensive curriculum, unmatched teaching methods, and career coaching to help you nail your next tech interview.

We offer 18 interview preparation courses, each tailored to a specific engineering domain or role, including the most in-demand and highest-paying domains and roles, such as:

### Try yourself in the Editor

Note: Input and Output will already be taken care of.

# Find All People With Secret Problem Statement

There are n people numbered from 0 to n - 1. A list of meetings is given whose each entry is a tuple [xi, yi, timei], indicating that the person xi and the person yi have a meeting at timei.
Person 0 has a secret and initially shares the secret with a given person at time 0. This secret is then shared every time a meeting takes place with the person that has the secret.
Return a list of all the people who have the secret after all the meetings have taken place.

## Example One

{
"n": 6,
"meetings": [
[1, 2, 3],
[2, 3, 5],
[1, 5, 2],
[3, 5, 10]
],
"first_person": 2
}

Output:

[0, 1, 2, 3, 5]

At time 0, person 0 shares the secret with person 2.
At time 2, neither person 1 nor person 5 knows the secret.
At time 3, person 2 shares the secret with person 1.
At time 5, person 2 shares the secret with person 3.
At time 10, person 3 shares the secret with person 5.
Thus, people 0, 1, 2, 3, and 5 know the secret after all the meetings.

## Example Two

{
"n": 5,
"meetings": [
[2, 3, 1],
[2, 4, 1],
[1, 2, 1]
],
"first_person": 1
}

Output:

[0, 1, 2, 3, 4]

At time 0, person 0 shares the secret with person 1.
At time 1, person 1 shares the secret with person 2, and person 2 shares the secret with persons 3 and 4.
Thus, people 0, 1, 2, 3, and 4 know the secret after all the meetings.

## Notes

• A person may attend multiple meetings at the same time.
• The secrets are shared instantaneously, which means a person may receive the secret and share it with people in other meetings within the same time frame.
• Return the output list in any order.

Constraints:

• 2 <= n <= 105
• 0 <= size of the input list <= 105
• 0 <= xi, yi <= n - 1
• xi != yi
• 1 <= timei <= 105
• 1 <= first person with whom secret is shared at time 0 <= n - 1

## Code For Find All People With Secret Solution: Bfs

/*
Asymptotic complexity in terms of the number of meetings \`m\` and the number of people \`n\`:
* Time: O(m * log(m) + n).
* Auxiliary space: O(n + m).
* Total space: O(n + m).
*/
vector<int> find_all_people_with_secret(int n, vector<vector<int>> &meetings, int first_person) {
vector<vector<pair<int,int>>> graph(n);
for (int i = 0; i < meetings.size(); i++) {
graph[meetings[i][0]].push_back({meetings[i][1], meetings[i][2]});
graph[meetings[i][1]].push_back({meetings[i][0], meetings[i][2]});
}

// Min Heap which stores a pair of elements where the first entry denotes the current
// time and the second entry denotes the person who knows the secret.
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
vector<int> is_secret_known(n, false);

pq.push({0, 0});
pq.push({0, first_person});

while (!pq.empty()) {
int current_time = pq.top().first;
int person_x = pq.top().second;
pq.pop();

if (is_secret_known[person_x]) {
continue;
}

is_secret_known[person_x] = true;

for (int i = 0; i < graph[person_x].size(); i++) {
int person_y = graph[person_x][i].first;
int meeting_time = graph[person_x][i].second;
if (is_secret_known[person_y]) {
continue;
}
if (meeting_time >= current_time) {
pq.push({meeting_time, person_y});
}
}
}

vector<int> people;
for (int i = 0; i < n; i++) {
if (is_secret_known[i]) {
people.push_back(i);
}
}

return people;
}

We hope that these solutions to the 4 sum problem have helped you level up your coding skills. You can expect problems like these at top tech companies like Amazon and Google.

If you are preparing for a tech interview at FAANG or any other Tier-1 tech company, register for Interview Kickstart’s FREE webinar to understand the best way to prepare.

Interview Kickstart offers interview preparation courses taught by FAANG+ tech leads and seasoned hiring managers. Our programs include a comprehensive curriculum, unmatched teaching methods, and career coaching to help you nail your next tech interview.

We offer 18 interview preparation courses, each tailored to a specific engineering domain or role, including the most in-demand and highest-paying domains and roles, such as: