Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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:
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 identified, or return -1 otherwise.
Example 1:
Input: n = 2, trust = [[1,2]] Output: 2
Example 2:
Input: n = 3, trust = [[1,3],[2,3]] Output: 3
Example 3:
Input: n = 3, trust = [[1,3],[2,3],[3,1]] Output: -1
Constraints:
1 <= n <= 10000 <= trust.length <= 104trust[i].length == 2trust are unique.ai != bi1 <= ai, bi <= nProblem summary: 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. 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 identified, or return -1 otherwise.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
2 [[1,2]]
3 [[1,3],[2,3]]
3 [[1,3],[2,3],[3,1]]
find-the-celebrity)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #997: Find the Town Judge
class Solution {
public int findJudge(int n, int[][] trust) {
int[] cnt1 = new int[n + 1];
int[] cnt2 = new int[n + 1];
for (var t : trust) {
int a = t[0], b = t[1];
++cnt1[a];
++cnt2[b];
}
for (int i = 1; i <= n; ++i) {
if (cnt1[i] == 0 && cnt2[i] == n - 1) {
return i;
}
}
return -1;
}
}
// Accepted solution for LeetCode #997: Find the Town Judge
func findJudge(n int, trust [][]int) int {
cnt1 := make([]int, n+1)
cnt2 := make([]int, n+1)
for _, t := range trust {
a, b := t[0], t[1]
cnt1[a]++
cnt2[b]++
}
for i := 1; i <= n; i++ {
if cnt1[i] == 0 && cnt2[i] == n-1 {
return i
}
}
return -1
}
# Accepted solution for LeetCode #997: Find the Town Judge
class Solution:
def findJudge(self, n: int, trust: List[List[int]]) -> int:
cnt1 = [0] * (n + 1)
cnt2 = [0] * (n + 1)
for a, b in trust:
cnt1[a] += 1
cnt2[b] += 1
for i in range(1, n + 1):
if cnt1[i] == 0 and cnt2[i] == n - 1:
return i
return -1
// Accepted solution for LeetCode #997: Find the Town Judge
impl Solution {
pub fn find_judge(n: i32, trust: Vec<Vec<i32>>) -> i32 {
let mut cnt1 = vec![0; (n + 1) as usize];
let mut cnt2 = vec![0; (n + 1) as usize];
for t in trust.iter() {
let a = t[0] as usize;
let b = t[1] as usize;
cnt1[a] += 1;
cnt2[b] += 1;
}
for i in 1..=n as usize {
if cnt1[i] == 0 && cnt2[i] == (n as usize) - 1 {
return i as i32;
}
}
-1
}
}
// Accepted solution for LeetCode #997: Find the Town Judge
function findJudge(n: number, trust: number[][]): number {
const cnt1: number[] = new Array(n + 1).fill(0);
const cnt2: number[] = new Array(n + 1).fill(0);
for (const [a, b] of trust) {
++cnt1[a];
++cnt2[b];
}
for (let i = 1; i <= n; ++i) {
if (cnt1[i] === 0 && cnt2[i] === n - 1) {
return i;
}
}
return -1;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.