Find the Town Judge

EasyGraphsdegree counting

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

A town has n people labelled 1 to n. You are given a list trust where trust[i] = [a, b] means person a trusts person b. Each pair appears at most once and nobody trusts themselves. The town judge, if one exists, is a person who trusts nobody and is trusted by every other person. There can be at most one such person. Return the judge's label, or -1 if no one satisfies both conditions.

Examples

Input: n = 3, trust = [[1,3],[2,3]]
Output: 3
Person 3 trusts nobody and is trusted by both 1 and 2.
Input: n = 3, trust = [[1,3],[2,3],[3,1]]
Output: -1
Person 3 trusts person 1, so 3 cannot be the judge, and nobody else is trusted by everyone.
Input: n = 1, trust = []
Output: 1
With a single person, they trust nobody and are vacuously trusted by all others.

Constraints

  • 1 <= n <= 1000
  • 0 <= trust.length <= 10^4
  • trust[i].length == 2
  • 1 <= a, b <= n and a != b
  • All pairs in trust are distinct

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.