The Celebrity Problem
Imagine there are 'N' people at a party, each assigned a unique ID from 0 to N-1. A celebrity at the party is a person who is known by everyone but knows no one else.
Problem Statement:
You are given a helper function knows(A, B)
that returns "true" if person with ID 'A' knows person 'B' and "false" otherwise. The task is to identify the celebrity at the party. If no celebrity exists, return -1.
Input:
The first line contains an integer 'T' representing the number of test cases. For each test case, the first line contains an integer 'N', which is the number of people at the party.
Output:
For each test case, output a single integer: the ID of the celebrity, or -1 if there is none.
Example:
Note:
- The knows function is pre-defined and should not be implemented by you.
Constraints:
- 1 <= T <= 50
- 2 <= N <= 104
- There is at most one celebrity at the party.
- There are at least 2 people at the party.
Additional Note:
- Minimize calls to the 'knows' function during implementation.
Be the first one to answer
Add answer anonymously...
Top Amazon Software Developer Intern interview questions & answers
Popular interview questions of Software Developer Intern
Top HR questions asked in Amazon Software Developer Intern
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app