FastPrepFind the Town Judge
Problems/Find the Town Judge

Find the Town Judge

Google logoGoogle● EasyINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report defines directed trust relationships and the two conditions for a judge. It describes scoring each person by indegree minus outdegree. It does not say how people are labeled, what to return when there is no judge, what happens with one person, whether relations are valid, the input limits, or a function signature. Our version is about 94% the same as the reported question.

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[][]) → int

Examples

Example 1

n = 2trust = [[1,2]]return = 2

Person 2 trusts nobody and is trusted by the only other person.

Example 2

n = 3trust = [[1,3],[2,3]]return = 3

Both other people trust person 3, and person 3 trusts nobody.

Example 3

n = 3trust = [[1,3],[2,3],[3,1]]return = -1

Person 3 is trusted by everyone else but trusts person 1, so there is no judge.

Example 4

n = 1trust = []return = 1

With one person, nobody else must trust them, and they trust nobody.

Constraints

  • 1 <= n <= 100000.
  • 0 <= trust.length <= 200000.
  • Every row in trust contains exactly two integers [a, b].
  • 1 <= a, b <= n and a != 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

Interview reports that mention this question

  • Google Software Engineer Onsite Questions: 10 Experiences

Google interview experiences

  • Google interviews · November 2026
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
  • Google Experienced SWE First Round and Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int findJudge(int n, int[][] trust) {
        // Return the judge's label, or -1 when no judge exists.
    }
}
Checking account…