Find the Town Judge (Graph representation)

Data Structures
Easy
Cisco
55.4K views

In a town of $N$ people, the judge is trusted by everyone else and trusts no one. Model the 'trusts' relationship as a graph and find the judge's node in $O(N)$ time.

Why Interviewers Ask This

Cisco evaluates this question to assess a candidate's ability to model real-world relationships as graph problems and optimize for linear time complexity. They specifically look for the skill of recognizing that tracking trust counts is more efficient than traversing edges, demonstrating practical algorithmic thinking over rote memorization.

How to Answer This Question

1. Clarify constraints: Confirm N people and the definition of the judge (trusted by all, trusts none). 2. Propose a frequency array approach instead of building an adjacency matrix to save space. 3. Iterate through the trust list once, incrementing a 'trustedBy' count for the receiver and decrementing or ignoring the giver. 4. Identify the candidate with a count exactly equal to N-1, ensuring they have zero outgoing edges implicitly. 5. Discuss edge cases like N=1 or no solution existing. This structured method shows you prioritize O(N) time and O(1) extra space, aligning with Cisco's focus on scalable network solutions.

Key Points to Cover

  • Recognize that explicit graph traversal is unnecessary when counting degrees suffices
  • Demonstrate optimization from O(N^2) adjacency matrix to O(N) frequency array
  • Explain the logic of using a single counter to represent both incoming and outgoing trust
  • Identify the specific condition where the final count must equal N-1
  • Address edge cases like N=1 or conflicting trust relationships explicitly

Sample Answer

To solve this efficiently, I would first clarify that we need to find a node with an in-degree of N-1 and an out-degree of 0. Building a full graph using an adjacency matrix would take O(N^2) space, which is unnecessary here. Instead, I propose using a single integer array of size N+1 to track net trust scores. As we iterate through the trust array, for every pair [a, b], we increment the score for b because they are trusted, and decrement the score for a because they cannot be the judge if they trust anyone. After processing all trust relationships in a single pass, we check if any person has a score exactly equal to N-1. If such a person exists, they are the judge; otherwise, no judge exists. For example, if N=3 and trust=[[1,3],[2,3]], person 3 gets +1 from 1 and +1 from 2, resulting in a score of 2, which equals N-1. This approach ensures O(N) time complexity and O(N) space complexity, meeting the strict performance requirements often seen in network routing scenarios at Cisco.

Common Mistakes to Avoid

  • Building a full adjacency matrix which wastes memory and increases time complexity unnecessarily
  • Failing to account for the judge trusting no one, leading to incorrect validation logic
  • Not checking if the potential judge actually exists before returning the result
  • Ignoring the O(N) constraint requirement and suggesting suboptimal sorting or traversal methods

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 154 Data Structures questionsBrowse all 27 Cisco questions