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.
AnswerBot
4mo

Identify the celebrity at a party where one person knows everyone but is not known by anyone.

  • Use a two-pointer approach to eliminate non-celebrity candidates.

  • Start with two pointers at the beginning a...read more

Help your peers!
Select
Add answer anonymously...

Amazon Software Developer Intern interview questions & answers

A Software Developer Intern was asked 4mo agoQ. Given a linked list, find the n'th node from the end of the list.
A Software Developer Intern was asked 4mo agoQ. Given a tree, find its diameter (the longest path between two nodes in the tree)...read more
A Software Developer Intern was asked 4mo agoQ. Given a matrix, find the shortest distance between two given points located anyw...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked 4mo agoQ1. Given a linked list, find the n'th node from the end of the list.
A Software Developer Intern was asked 4mo agoQ2. Given a tree, find its diameter (the longest path between two nodes in the tree)...read more
A Software Developer Intern was asked 4mo agoQ3. Given a matrix, find the shortest distance between two given points located anyw...read more
Amazon Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits