As reported: American Express Engineer II, round 1 (technical, two interviewers, 2025): a variant of the celebrity problem, solved with a stack.
There are n people at a party, labelled 0 to n − 1. A celebrity is someone who knows nobody else, while all the other n − 1 people know them. There is at most one celebrity.
You may only ask questions of the form "does a know b?" through the helper knows(a, b). Return the celebrity's label, or -1 if there is no celebrity. Ask as few questions as possible.
This is LeetCode 277 — Find the Celebrity.
Example 1
graph = [[1,1,0],[0,1,0],[1,1,1]] -> 1
graph[i][j] = 1 means i knows j. Persons 0 and 2 both know 1, and 1 knows nobody else.
Example 2
graph = [[1,0,1],[1,1,0],[0,1,1]] -> -1
Constraints: 2 ≤ n ≤ 100; graph[i][j] is 0 or 1; graph[i][i] = 1.
Code runner format: one line: graph as a JSON matrix. The harness builds knows(a, b) from it and calls findCelebrity(n); implement findCelebrity using only knows, and the harness prints the returned label.