Identify a Person Trusted by Everyone Else Who Trusts Nobody

Quick Overview

Identify a Person Trusted by Everyone Else Who Trusts Nobody. People are labeled1 through n. Each pair [a,b] in trust means a trusts b, and no unlisted relationship exists.

Identify a Person Trusted by Everyone Else Who Trusts Nobody

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem People are labeled1 through n. Each pair `[a,b]` in `trust` means a trusts b, and no unlisted relationship exists. Return the label of the person who trusts nobody and is trusted by every other person, or-1 if no such unique person exists. ```cpp class Solution { public: int findJudge(int n, vector<vector<int>>& trust); }; ``` ### Examples **Example 1:** ```text Input: n = 4, trust = [[1,4],[2,4],[3,4]] Output: 4 Explanation: Everyone else trusts4, and4 has no outgoing trust. ``` **Example 2:** ```text Input: n = 1, trust = [] Output: 1 Explanation: The only person trusts nobody and vacuously has trust from every other person. ``` ### Constraints - `1 <= n <= 1000`. - `0 <= trust.length <= 10^4`; each pair has length2. - All pairs are unique and contain distinct labels a!=b. - `1 <= a,b <= n`. ### Contract considerations The given edge list is complete, and a qualifying label is automatically unique. ```hint Read the boundary condition Translate the two judge properties into incoming and outgoing edge counts. ``` ### Boundary reminders The given edge list is complete, and a qualifying label is automatically unique.

Overview: Identify a Person Trusted by Everyone Else Who Trusts Nobody. People are labeled1 through n. Each pair [a,b] in trust means a trusts b, and no unlisted relationship exists.

Amazon logo
Amazon
Aug 30, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

People are labeled1 through n. Each pair [a,b] in trust means a trusts b, and no unlisted relationship exists. Return the label of the person who trusts nobody and is trusted by every other person, or-1 if no such unique person exists.

class Solution {
public:
    int findJudge(int n, vector<vector<int>>& trust);
};

Examples

Example 1:

Input: n = 4, trust = [[1,4],[2,4],[3,4]]
Output: 4
Explanation: Everyone else trusts4, and4 has no outgoing trust.

Example 2:

Input: n = 1, trust = []
Output: 1
Explanation: The only person trusts nobody and vacuously has trust from every other person.

Constraints

  • 1 <= n <= 1000 .
  • 0 <= trust.length <= 10^4 ; each pair has length2.
  • All pairs are unique and contain distinct labels a!=b.
  • 1 <= a,b <= n .

Contract considerations

The given edge list is complete, and a qualifying label is automatically unique.

Boundary reminders

The given edge list is complete, and a qualifying label is automatically unique.