Find the Celebrity from Pairwise Acquaintances
Updated
Problem statement
A group contains n people numbered from 0 to n - 1. The matrix entry knows[a][b] is 1 exactly when person a knows person b.
A celebrity is known by every other person but knows no other person. At most one celebrity exists. Return the celebrity's index, or -1 if the group has no celebrity. Diagonal entries are irrelevant.
Function
findCelebrity(knows: int[][]) → intExamples
Example 1
knows = [[0,1,0],[0,0,0],[0,1,0]]return = 1People 0 and 2 know person 1, while person 1 knows neither of them.
Example 2
knows = [[0,1],[1,0]]return = -1Each person knows the other, so neither can be a celebrity.
Example 3
knows = [[1]]return = 0With one person, all off-diagonal celebrity conditions hold vacuously.
Constraints
1 <= n <= 2000.knowsis ann x nmatrix.- Every entry is
0or1. - Only off-diagonal entries determine the result.
More Salesforce Onsite Interview questions
- Count Increasing TripletsONSITE INTERVIEW · Seen Nov 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Reverse a Sublist of a Linked ListONSITE INTERVIEW · Seen Nov 2026
- Rotting OrangesONSITE INTERVIEW · Seen Nov 2026
- 3Sum SmallerONSITE INTERVIEW · Seen Nov 2026
- Count Zero Request ServersONSITE INTERVIEW · Seen Nov 2026
- Group Identical Tree LevelsONSITE INTERVIEW · Seen Oct 2026
- Meeting Rooms IIONSITE INTERVIEW · Seen Oct 2026
- Construct a Tree from Level-Order and Inorder TraversalsONSITE INTERVIEW · Seen Sep 2026
- Maximize Monsters DefeatedONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Find the Celebrity from Pairwise AcquaintancesAmazon · ONSITE INTERVIEW
- Minimum Operations to Sort with Rotations Then SwapsBank of New York Mellon · OA
- Optimal TransferSnowflake · OASnowflake interview questions
- Maximum XOR Elimination ScoreRippling · OARippling interview questions
- Minimum Piano Hand Raises and Thumb PositionsGoogle · ONSITE INTERVIEWGoogle interview questions
- Fair Capped Money DistributionGusto · PHONE SCREEN · ONSITE INTERVIEWGusto interview questions
- Mutual-Friend RecommendationsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Tennis Court SchedulingAtlassian · PHONE SCREEN · ONSITE INTERVIEWAtlassian interview questions
- Minimum Station Tracks for a Train ScheduleBNSF Railway · ONSITE INTERVIEWBNSF Railway interview questions
- Best Time to Buy and Sell Stock IIKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions