Find the Town Judge
Updated
Problem statement
There are n people labeled from 1 through n. Each row trust[i] = [a, b] means that person a trusts person b.
A person is the town judge when both conditions hold:
- The person trusts nobody.
- Every other person trusts that person.
Return the judge's label. Return -1 when no person satisfies both conditions. When n = 1 and there are no trust relationships, person 1 is the judge.
Interview follow-ups
- Transitive trust: if trust extends along directed paths, discuss how to find the person reachable from the greatest number of distinct other people. Cycles and ties must be handled explicitly.
- Multiple judges: explain whether more than one person can satisfy the original two judge conditions, and how the output would change if the interviewer relaxes those conditions and asks for every qualifying person.
The function you write uses only the original direct-trust definition above.
Function
findJudge(n: int, trust: int[][]) → intExamples
Example 1
n = 2trust = [[1,2]]return = 2Person 2 trusts nobody and is trusted by the only other person.
Example 2
n = 3trust = [[1,3],[2,3]]return = 3Both other people trust person 3, and person 3 trusts nobody.
Example 3
n = 3trust = [[1,3],[2,3],[3,1]]return = -1Person 3 is trusted by everyone else but trusts person 1, so there is no judge.
Example 4
n = 1trust = []return = 1With one person, nobody else must trust them, and they trust nobody.
Constraints
1 <= n <= 100000.0 <= trust.length <= 200000.- Every row in
trustcontains exactly two integers[a, b]. 1 <= a, b <= nanda != b.- Every ordered pair
[a, b]appears at most once.
More Google Onsite Interview questions
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Mutual-Friend RecommendationsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Jump Game IVZomato / Eternal · ONSITE INTERVIEWZomato / Eternal interview questions
- Delete and Compact a Forest SubtreePinterest · PHONE SCREEN · ONSITE INTERVIEWPinterest interview questions
- Island Count and Maximum AreaOracle · ONSITE INTERVIEWOracle interview questions
- Radar Barrier CrossingSnap Inc. · ONSITE INTERVIEWSnap Inc. interview questions
- Find the Celebrity from Pairwise AcquaintancesAmazon · ONSITE INTERVIEW
- Shortest Increasing Path to a TargetUber · ONSITE INTERVIEWUber interview questions
- Find the Celebrity from Pairwise AcquaintancesSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Find All People With SecretIDFC Bank · ONSITE INTERVIEWIDFC Bank interview questions
- Cheapest Flights Within K StopsGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions