The Celebrity Problem is a classic interview question often associated with LinkedIn, Google, and other tech companies, involving array representations, two-pointer techniques, and graph modeling (where people are nodes and "knows" relations are directed edges).[1][3][5]
In a party of n people (labeled 0 to n-1), a celebrity is defined as someone who knows no one (out-degree 0) but is known by everyone else (in-degree n-1). You are given a function knows(a, b) (or an n x n adjacency matrix M where M[i][j] = 1 if person i knows j, else 0) that returns true if a knows b. Find and return the celebrity's index (0-based); return -1 if none exists. The function knows(a, a) returns 1 (self-knowledge), but this doesn't affect celebrity status since celebrities know no others.[3][4][5][1]
Example 1
Input: n = 4, M = [,,, ][1]
Example 2
Input: n = 2, M = [, ][1]
Example 3
Input: n = 3 (via knows function calls implying relations where 1 is known by 0 and 2 but knows none).
Output: 1[5]