Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

997. Find the Town Judge

Easy49.8% Acceptance
ArrayHash TableGraph
Asked by:
G
Google
ProblemSolutions (12)VideosCompanies (4)Notes

Problem Statement

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:

  1. The town judge trusts nobody.
  2. Everybody (except for the town judge) trusts the town judge.
  3. 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.

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 <= 1000
  • 0 <= trust.length <= 104
  • trust[i].length == 2
  • All the pairs of trust are unique.
  • ai != bi
  • 1 <= ai, bi <= n
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
A
Amazon
A
Apple
A
Adobe

Approach

The key observation in #997 Find the Town Judge is that the judge has two defining properties: they trust nobody and are trusted by everyone else. If there are n people in the town, the judge must have exactly n-1 people trusting them.

A practical way to model this is using a graph perspective. Each trust pair [a, b] represents a directed edge from a to b. The judge will have in-degree = n-1 and out-degree = 0. Instead of storing full adjacency structures, you can maintain a single score array where trusting someone decreases a person's score and being trusted increases it. The judge will end with a score of n-1.

This approach efficiently processes each trust relationship once, giving linear time complexity. It also uses only a small auxiliary array, making the space usage minimal.

Complexity

ApproachTime ComplexitySpace Complexity
In-degree / Trust Score ArrayO(n + m)O(n)

Video Solution Available

Nick White

View all video solutions

Solutions (12)

In-Degree and Out-Degree Calculation

The idea is to calculate the in-degree and out-degree for each person. A person is the town judge if their in-degree is n-1 (trusted by all other n-1 people) and their out-degree is 0 (trusting no one).

Time Complexity: O(trust.length), where trust.length is the total number of trust relationships.
Space Complexity: O(n), where n is the number of people in the town.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <stdlib.h>
3
4int findJudge(int n, int** trust, int trustSize, int* trustColSize) {
5    int* inDegrees = (int*)calloc(n + 1, sizeof(int));
6    int* outDegrees = (int*)calloc(n + 1, sizeof(int));
7
8    for (int i = 0; i < trustSize; i++) {
9        int a = trust[i][0];
10        int b = trust[i][1];
11        outDegrees[a]++;
12        inDegrees[b]++;
13    }
14
15    for (int i = 1; i <= n; i++) {
16        if (inDegrees[i] == n - 1 && outDegrees[i] == 0) {
17            free(inDegrees);
18            free(outDegrees);
19            return i;
20        }
21    }
22
23    free(inDegrees);
24    free(outDegrees);
25    return -1;
26}
27

Explanation

This C program uses two arrays to store in-degrees and out-degrees of each person. It then iterates through the trust array to update these values accordingly. Finally, it searches for a person whose in-degree is n-1 and out-degree is 0, identifying them as the judge if they exist.

Graph Representation Using Single Array

An alternative approach is to use a single array to calculate the difference between in-degrees and out-degrees. For the judge, this difference should be n-1.

Time Complexity: O(trust.length)
Space Complexity: O(n)

CC++JavaPythonC#JavaScript
1#include 



Video Solutions

Watch expert explanations and walkthroughs

LeetCode 997. Find The Town Judge (Algorithm Explained)

Nick White
6:0622,298 views

Asked By Companies

4 companies
G
Google
A
Amazon
A
Apple
A
Adobe

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Two SumEasy
Valid SudokuMedium
Sudoku SolverHard
First Missing PositiveHard
More similar problems

Related Topics

ArrayHash TableGraph

Problem Stats

Acceptance Rate49.8%
DifficultyEasy
Companies4

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Find the Town Judge a graph problem?

Yes, it can be viewed as a directed graph problem where trust relationships represent edges. The town judge corresponds to a node with in-degree n-1 and out-degree 0.

Is Find the Town Judge asked in coding interviews?

Yes, this problem is commonly used in coding interviews to test understanding of graph basics, counting techniques, and edge-case reasoning. It is especially popular for beginner-level interview practice.

What data structure is best for Find the Town Judge?

An integer array is typically sufficient to track trust scores for each person. This avoids building a full graph structure and keeps the solution simple and efficient.

What is the optimal approach for Find the Town Judge?

The optimal approach uses a trust score or in-degree/out-degree concept. For every trust pair, decrease the score of the person who trusts and increase the score of the person being trusted. The town judge will have a final score of n-1.

<stdio.h>
2
#
include
<stdlib.h>
3
4
int
findJudge
(
int
n
,
int
*
*
trust
,
int
trustSize
,
int
*
trustColSize
)
{
5
int
*
netTrust
=
(
int
*
)
calloc
(
n
+
1
,
sizeof
(
int
)
)
;
6
7
for
(
int
i
=
0
;
i
<
trustSize
;
i
++
)
{
8
int
a
=
trust
[
i
]
[
0
]
;
9
int
b
=
trust
[
i
]
[
1
]
;
10
netTrust
[
a
]
--
;
11
netTrust
[
b
]
++
;
12
}
13
14
for
(
int
i
=
1
;
i
<=
n
;
i
++
)
{
15
if
(
netTrust
[
i
]
==
n
-
1
)
{
16
free
(
netTrust
)
;
17
return
i
;
18
}
19
}
20
21
free
(
netTrust
)
;
22
return
-
1
;
23
}
24

Explanation

This C implementation uses a single array to track the net trust value of each person. A candidate judge should have a net positive trust equal to n-1.