Find the Node with the Highest Edge Score in a Graph

Quick Overview

Given a directed graph where every node has exactly one outgoing edge, stored as an array, find the node whose incoming neighbors have the largest sum of labels, breaking ties by the smallest index. It tests reading a graph from an array, aggregating over incoming edges, and handling ties and large sums correctly.

Find the Node with the Highest Edge Score in a Graph

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a directed graph with `n` nodes labeled from `0` to `n - 1`, where each node has exactly one outgoing edge. The graph is represented by a 0-indexed integer array `edges` of length `n`, where `edges[i]` indicates that there is a directed edge from node `i` to node `edges[i]`. The edge score of a node `i` is the sum of the labels of all nodes that have an edge pointing to node `i`. Return the node with the highest edge score. If multiple nodes have the same edge score, return the node with the smallest index. ### Function Signature ```python def edge_score(edges: list[int]) -> int: ``` ### Rules - A node with no incoming edges has an edge score of 0. - Labels start at 0, so an edge from node 0 adds 0 to the score of the node it points to. - When several nodes share the highest edge score, return the smallest of their indices. ### Constraints - `n == len(edges)` - `2 <= n <= 10^5` - `0 <= edges[i] < n` - `edges[i] != i` ### Examples ```text Input: edges = [1,0,0,0,0,7,7,5] Output: 7 ``` Nodes 1, 2, 3 and 4 point to node 0, so its score is 1 + 2 + 3 + 4 = 10. Nodes 5 and 6 point to node 7 for a score of 11, node 7 points to node 5 for a score of 7, and node 0 points to node 1 for a score of 0, so node 7 has the highest score. ```text Input: edges = [2,0,0,2] Output: 0 ``` Node 0 receives edges from nodes 1 and 2 (score 3), and node 2 receives edges from nodes 0 and 3 (score 0 + 3 = 3). The two scores tie, so the smaller index, 0, is returned.

Overview: Given a directed graph where every node has exactly one outgoing edge, stored as an array, find the node whose incoming neighbors have the largest sum of labels, breaking ties by the smallest index. It tests reading a graph from an array, aggregating over incoming edges, and handling ties and large sums correctly.

Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a directed graph with n nodes labeled from 0 to n - 1, where each node has exactly one outgoing edge.

The graph is represented by a 0-indexed integer array edges of length n, where edges[i] indicates that there is a directed edge from node i to node edges[i].

The edge score of a node i is the sum of the labels of all nodes that have an edge pointing to node i.

Return the node with the highest edge score. If multiple nodes have the same edge score, return the node with the smallest index.

Function Signature

def edge_score(edges: list[int]) -> int:

Rules

  • A node with no incoming edges has an edge score of 0.
  • Labels start at 0, so an edge from node 0 adds 0 to the score of the node it points to.
  • When several nodes share the highest edge score, return the smallest of their indices.

Constraints

  • n == len(edges)
  • 2 <= n <= 10^5
  • 0 <= edges[i] < n
  • edges[i] != i

Examples

Input: edges = [1,0,0,0,0,7,7,5]
Output: 7

Nodes 1, 2, 3 and 4 point to node 0, so its score is 1 + 2 + 3 + 4 = 10. Nodes 5 and 6 point to node 7 for a score of 11, node 7 points to node 5 for a score of 7, and node 0 points to node 1 for a score of 0, so node 7 has the highest score.

Input: edges = [2,0,0,2]
Output: 0

Node 0 receives edges from nodes 1 and 2 (score 3), and node 2 receives edges from nodes 0 and 3 (score 0 + 3 = 3). The two scores tie, so the smaller index, 0, is returned.