The O(n) constraint is what makes this interesting.
Use a two-phase elimination strategy: first, find a candidate celebrity by iterating through all people and eliminating anyone who is known by someone else, which takes at most n-1 API calls. Then, verify the candidate by checking that they know no one and everyone knows them, which takes at most 2(n-1) API calls. If verification fails, return -1.
Pro tip: Emphasize that the elimination phase reduces the candidate set to one person in linear time, and the verification phase is crucial to avoid false positives. Mention that the total API calls are bounded by 3n-3, which is O(n).
Clarify that the API knows(A, B) returns true if A knows B, and that a celebrity knows no one and is known by everyone. Note that there can be at most one celebrity.
Initialize candidate = 0. For each person i from 1 to n-1, if knows(candidate, i) is true, then candidate cannot be a celebrity (since a celebrity knows no one), so set candidate = i. Otherwise, i cannot be a celebrity (since a celebrity is known by everyone), so keep candidate. This uses n-1 API calls.
For the candidate, check that they know no one: for each person i, if knows(candidate, i) is true, return -1. Also check that everyone knows the candidate: for each person i, if i != candidate and knows(i, candidate) is false, return -1. This uses at most 2(n-1) API calls.
If both checks pass, return the candidate as the celebrity. Otherwise, return -1.
Discuss that total API calls are O(n), and handle edge cases like n=0 or n=1. For n=1, the single person is a celebrity by definition (knows no one and is known by everyone vacuously).
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.