Node With Highest Edge Score - Problem

You are given a directed graph with n nodes labeled from 0 to n-1, where each node has exactly one outgoing edge. This creates a functional graph where every node points to exactly one other node (possibly itself).

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 defined as the sum of the labels of all the nodes that have an edge pointing to i. In other words, it's the sum of all indices j where edges[j] == i.

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

Example: If edges = [1,0,0,0,0,7,7,5], then:

  • Node 0 receives edges from nodes 1,2,3,4 → score = 1+2+3+4 = 10
  • Node 1 receives edge from node 0 → score = 0
  • Node 7 receives edges from nodes 5,6 → score = 5+6 = 11

Node 7 has the highest score (11), so we return 7.

Input & Output

example_1.py — Basic Case
$ Input: edges = [1,0,0,0,0,7,7,5]
Output: 7
💡 Note: Node 0 receives edges from nodes 1,2,3,4 (score = 1+2+3+4 = 10). Node 1 receives edge from node 0 (score = 0). Node 5 receives edge from node 7 (score = 7). Node 7 receives edges from nodes 5,6 (score = 5+6 = 11). Node 7 has the highest score of 11.
example_2.py — Tie Breaker
$ Input: edges = [2,0,0,2]
Output: 0
💡 Note: Node 0 receives edges from nodes 1,2 (score = 1+2 = 3). Node 2 receives edges from nodes 0,3 (score = 0+3 = 3). Both nodes have score 3, but node 0 has smaller index, so we return 0.
example_3.py — Self Loop
$ Input: edges = [0]
Output: 0
💡 Note: Only one node exists. Node 0 points to itself, so it receives an edge from node 0. Score of node 0 = 0. Return 0.

Constraints

  • n == edges.length
  • 2 ≤ n ≤ 105
  • 0 ≤ edges[i] ≤ n - 1
  • edges[i] ≠ i
  • Edge scores can be very large - use appropriate data types to avoid overflow

Visualization

Tap to expand
Edge Score Calculation Process🗳️ Voting Analogy: Each node votes for another nodeVote value = voter's ID number | Goal: Find candidate with highest total vote valueStep 1: Initialize Scores Arrayscores = [0, 0, 0, 0, 0, 0, 0, 0] // One score counter per nodeStep 2: Process Each Vote (Edge)Node 0 votes for node 1: scores[1] += 0 → scores[1] = 0Node 1 votes for node 0: scores[0] += 1 → scores[0] = 1Node 2 votes for node 0: scores[0] += 2 → scores[0] = 3Step 3: Find WinnerFinal scores: [10, 0, 0, 0, 0, 7, 0, 11]🏆 Node 7 wins with score 11!
Understanding the Visualization
1
Initialize Score Array
Create an array to track accumulated scores for each node
2
Process Each Edge
For edge i → edges[i], add voter ID (i) to candidate's score (edges[i])
3
Find Winner
Scan scores to find maximum value, return smallest index if tied
Key Takeaway
🎯 Key Insight: Instead of repeatedly searching for each node's incoming edges, we can calculate all scores in one pass by directly adding each source node's contribution to its target's running total.
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
25.5K Views
Medium Frequency
~12 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen