Find a Celebrity from a Knows Matrix
Company: Salesforce
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Overview: Find a celebrity in a knows matrix with linear candidate elimination followed by complete verification that the candidate knows nobody and is known by everyone else.
Constraints
- 0 <= n <= 10000.
- The knows matrix is square.
- Diagonal entries are unrestricted and ignored.
- A celebrity c must know nobody else and be known by everybody else.
- Return -1 rather than an unverified candidate.
Examples
Input: ([[False, True, True], [False, False, True], [False, False, False]],)
Expected Output: 2
Explanation: This is the source example; person two knows nobody else and is known by both others.
Input: ([] ,)
Expected Output: -1
Explanation: No people means no celebrity.
Hints
- Compare candidates pairwise so each matrix lookup eliminates one person.
- The last possible candidate still needs a full incoming and outgoing verification pass.