997. Find the Town Judge
easyAsked at CiscoFind the Town Judge at Cisco is a graph in-degree / out-degree problem disguised as a trust puzzle. The interviewer is checking whether you can recast 'judge = everyone trusts them, they trust no one' as 'in-degree N-1, out-degree 0' on a directed graph.
By Sam K., Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Cisco loops.
- Glassdoor (2026-Q1)— Cisco SDE-I and SDE-II reports cite this as a 20-minute warm-up graph round.
- Reddit r/leetcode (2025-11)— Cisco interview write-up lists Find the Town Judge as a recurring problem.
Problem
In a town, there are n people labeled from 1 to n. There is a rumor that one of these people is secretly the town judge. If the town judge exists, then: the town judge trusts nobody, everybody (except for the town judge) trusts the town judge, and there is exactly one person that satisfies properties 1 and 2. You are given an array trust where trust[i] = [ai, bi] representing that the person labeled ai trusts the person labeled bi. If a trust relationship does not exist in trust array, then such a trust relationship does not exist. Return the label of the town judge if the town judge exists and can be uniquely identified, or return -1 otherwise.
Constraints
1 <= n <= 10000 <= trust.length <= 10^4trust[i].length == 2All the pairs of trust are unique.ai != bi1 <= ai, bi <= n
Examples
Example 1
n = 2, trust = [[1,2]]2Explanation: 1 trusts 2, 2 trusts no one → 2 is the judge.
Example 2
n = 3, trust = [[1,3],[2,3]]3Example 3
n = 3, trust = [[1,3],[2,3],[3,1]]-1Explanation: 3 trusts 1, so 3 can't be the judge.
Approaches
1. Two-array brute force
Build separate trustsCount (out-degree) and trustedByCount (in-degree) arrays. The judge has trustedByCount[judge] == n-1 AND trustsCount[judge] == 0.
- Time
- O(E + N)
- Space
- O(N)
function findJudgeBrute(n, trust) {
const trusts = new Array(n + 1).fill(0);
const trustedBy = new Array(n + 1).fill(0);
for (const [a, b] of trust) {
trusts[a]++;
trustedBy[b]++;
}
for (let i = 1; i <= n; i++) {
if (trusts[i] === 0 && trustedBy[i] === n - 1) return i;
}
return -1;
}Tradeoff: Clear two-step intent — separate counters for in and out degree. Correct, linear time. Slightly more space than necessary.
2. Net-trust single-array (optimal)
Use ONE array `net`. For each edge [a, b]: net[a]--; net[b]++. The judge is the unique node with net[i] == n-1.
- Time
- O(E + N)
- Space
- O(N)
function findJudge(n, trust) {
const net = new Array(n + 1).fill(0);
for (const [a, b] of trust) {
net[a]--;
net[b]++;
}
for (let i = 1; i <= n; i++) {
if (net[i] === n - 1) return i;
}
return -1;
}Tradeoff: Same Big-O, half the memory. The math: judge's net = (n-1) trusted_by - 0 trusts = n-1. Any non-judge has at MOST (n-2) - 1 = n-3 net (trusted by everyone except themselves and the judge, and they trust the judge), so n-1 uniquely identifies the judge.
Cisco-specific tips
Cisco interviewers grade this on whether you recast the problem as 'in-degree N-1, out-degree 0.' Say that sentence first. Then offer the net-trust optimization: 'Since the judge's signature is in - out = n-1, I can collapse to one array.' The math justification — 'no non-judge can have net = n-1 because they trust at least one person AND aren't trusted by themselves' — earns full marks.
Common mistakes
- Using a 0-indexed array when people are 1-indexed — off-by-one returns -1 on valid inputs.
- Forgetting the n=1 edge case — trust is empty, person 1 is trivially the judge (n-1 = 0 trustees).
- Checking only `trustedBy[i] === n-1` and missing the `trusts[i] === 0` half — fails when someone is trusted by all but also trusts someone.
Follow-up questions
An interviewer at Cisco may pivot to one of these next:
- Find the Celebrity (LC 277 premium) — same in-degree/out-degree idea but only `knows(a, b)` queries available, must minimize queries.
- What if there can be multiple judges? (Return list of all nodes with net = n-1.)
- What if trust is weighted? (Now it's about ranking, not single max — sort by weighted in-out.)
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why does the net = n-1 uniqueness work?
Judge has +n-1 (everyone trusts them) and -0 (they trust no one) → net = n-1. Any non-judge i has at most +n-2 incoming (judge doesn't trust them, themselves doesn't count) and AT LEAST -1 outgoing (they trust the judge if one exists) → net ≤ n-3. So n-1 cleanly identifies the judge.
Is this really a 'graph' problem or just counting?
It IS counting — but the FRAMING as a directed graph with in/out-degree is what makes the algorithm obvious. Cisco interviewers want you to say 'I'll model trust as a directed edge a -> b and the judge is the node with in-degree n-1 and out-degree 0.' That graph framing is the signal.
Free learning resources
Curated free links for this problem.