Spot the VIP in a Social Network
Trees & GraphsAuto-graded · 18 test casesLast updated
Tests whether a candidate can exploit a single elimination insight to find a universal acquaintance in O(n) time instead of O(n²).
Problem Statement
At a networking event with n attendees (numbered 0 through n-1), there may be a VIP: someone that every other attendee recognizes, but who recognizes nobody in return.
You are given a square grid acquainted of size n × n, where acquainted[i][j] == 1 means person i recognizes person j, and 0 means they do not.
Return the index of the VIP. If no such person exists, return -1.
A correct algorithm must do better than the brute-force O(n²) scan.
Constraints
1 ≤ n ≤ 2000acquainted[i][i] == 0for alli(no one is listed as recognizing themselves)acquaintedis a list of lists of integers, each value either0or1- There is at most one VIP (uniqueness is guaranteed by the definition)
- A single attendee (
n == 1) is trivially the VIP
Examples
Example 1
Input: find_vip([[0, 1, 0], [0, 0, 0], [0, 1, 0]])
Output: 1
Example 2
Input: find_vip([[0, 1], [1, 0]])
Output: -1
Example 3
Input: find_vip([[0]])
Output: 0
Hints
Hint 1
Each entry acquainted[i][j] lets you immediately rule out exactly one of the two people as the VIP — think about which one is eliminated in each case.
Hint 2
A single forward pass through all attendees can narrow the field down to one surviving candidate using only n-1 lookups total.
Hint 3
The survivor of the first pass is not yet confirmed. You still need to verify both directions — that they recognize nobody and that everybody recognizes them — before returning their index.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The Core Insight
Brute-force checks every ordered pair, costing O(n²). The faster path comes from one observation: a single lookup acquainted[a][b] always rules out at least one of the two people as the VIP.
- If
acquainted[a][b] == 1, thenarecognizes someone — soacannot be the VIP. - If
acquainted[a][b] == 0, thenbis not recognized bya— sobcannot be the VIP (the VIP must be recognized by everyone).
In both cases, exactly one person is eliminated per lookup. That means n-1 lookups are enough to eliminate all but one candidate.
Two-Pass Algorithm
Pass 1 — Elimination.
Start with candidate = 0. Walk through every other attendee. Ask: does candidate recognize this person?
- Yes → the candidate is disqualified; the current person takes over as candidate.
- No → the current person is disqualified; the candidate stays.
After the loop, one person remains who has never been disqualified.
Pass 2 — Verification.
The survivor only avoided being ruled out — that does not confirm they are the VIP. Check every other attendee p:
acquainted[candidate][p]must be0(candidate recognizes no one).acquainted[p][candidate]must be1(everyone recognizes the candidate).
If any check fails, return -1.
Why This Is Correct
Invariant: after processing index p in the first loop, no one in positions 0 through p other than candidate can be the VIP.
- Base case: trivially true with only person 0 considered.
- If the candidate recognised
p: the old candidate is out; every earlier person was already out;pbecomes the only remaining possibility. - If the candidate did not recognise
p:pis out; the candidate and the invariant both hold.
So if a VIP exists, it must be candidate after the loop. The verification pass is what actually confirms (or denies) this — the first pass alone is not sufficient. For example, with [[0,1],[1,0]], person 1 survives the first pass but verification reveals that person 1 recognises person 0, so -1 is returned correctly.
Uniqueness: at most one VIP can exist. If both A and B were VIPs, then everyone (including A) must recognise B, but A as a VIP recognises no one — a contradiction.
Edge Cases
n == 1([[0]]): the elimination loop never runs; verification has no one to check; returns0. A lone attendee is vacuously a VIP.n == 0: returns-1defensively.- No relationships at all (
n > 1): candidate stays0; verification fails because person1does not recognise0; returns-1. - VIP at index
0orn-1: both are handled correctly by the invariant.
Complexity
- Time: O(n). The first pass makes
n-1lookups; the second makes at most2(n-1). Total: at most3(n-1)matrix reads, regardless of the n² input size. - Space: O(1) extra.
Full Solution
from typing import List
def find_vip(acquainted: List[List[int]]) -> int:
n = len(acquainted)
if n == 0:
return -1
# Pass 1: elimination.
# acquainted[candidate][person] == 1 -> candidate recognizes someone, ruled out.
# acquainted[candidate][person] == 0 -> person is not recognized by candidate, ruled out.
# Either way, exactly one person is eliminated per step.
candidate = 0
for person in range(1, n):
if acquainted[candidate][person] == 1:
candidate = person
# Pass 2: confirm both VIP conditions for the sole survivor.
for person in range(n):
if person == candidate:
continue
# Candidate must not recognize anyone, and everyone must recognize candidate.
if acquainted[candidate][person] != 0 or acquainted[person][candidate] != 1:
return -1
return candidate