Find the largest cycle sum in a directed graph where each vertex has at most one outgoing edge

Quick Overview

Given a directed graph where each vertex has at most one outgoing edge, return the largest sum of vertex indices over all cycles, or -1 when there is none. It tests cycle detection in functional graphs, separating tails from cycles, and a linear-time iterative walk.

Find the largest cycle sum in a directed graph where each vertex has at most one outgoing edge

Company: Juspay

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a directed graph of `n` vertices numbered from `0` to `n - 1`, where each vertex has at most one outgoing edge. The graph is represented by a 0-indexed integer array `edges` of size `n`, where `edges[i]` is the vertex that vertex `i` points to. If vertex `i` has no outgoing edge, then `edges[i] == -1`. Self-loops (`edges[i] == i`) are allowed. The sum of a cycle is the sum of the indices of all vertices that lie on the cycle. Vertices on a path that leads into a cycle, but are not part of it, are not included. Return the maximum sum of any cycle in the graph. If no cycle exists, return `-1`. The answer may exceed the range of a 32-bit integer, so languages with fixed-width integers need a 64-bit result. ### Function Signature ```python def largest_sum_cycle(edges: list[int]) -> int: ``` ### Rules - A self-loop is a cycle of one vertex, and its sum is that vertex's index. A self-loop on vertex `0` is a cycle with sum `0`, which is a valid answer and different from `-1`. ### Constraints - `n == len(edges)` - `1 <= n <= 10^5` - `-1 <= edges[i] < n` - Self-loops (`edges[i] == i`) are allowed. ### Examples ```text Input: edges = [2,0,1,4,5,3,4] Output: 12 ``` Vertices 0, 2 and 1 form the cycle `0 -> 2 -> 1 -> 0` with sum 3. Vertices 3, 4 and 5 form the cycle `3 -> 4 -> 5 -> 3` with sum 12. Vertex 6 only leads into the second cycle and is not counted. The larger sum is 12. ```text Input: edges = [1,2,-1,2] Output: -1 ``` The walks `0 -> 1 -> 2` and `3 -> 2` both end at vertex 2, which has no outgoing edge, so the graph has no cycle. ```text Input: edges = [0,0] Output: 0 ``` Vertex 0 points to itself, so it is a one-vertex cycle with sum 0. Vertex 1 leads into it and is not counted.

Overview: Given a directed graph where each vertex has at most one outgoing edge, return the largest sum of vertex indices over all cycles, or -1 when there is none. It tests cycle detection in functional graphs, separating tails from cycles, and a linear-time iterative walk.

Juspay logo
Juspay
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a directed graph of n vertices numbered from 0 to n - 1, where each vertex has at most one outgoing edge. The graph is represented by a 0-indexed integer array edges of size n, where edges[i] is the vertex that vertex i points to. If vertex i has no outgoing edge, then edges[i] == -1. Self-loops (edges[i] == i) are allowed.

The sum of a cycle is the sum of the indices of all vertices that lie on the cycle. Vertices on a path that leads into a cycle, but are not part of it, are not included.

Return the maximum sum of any cycle in the graph. If no cycle exists, return -1. The answer may exceed the range of a 32-bit integer, so languages with fixed-width integers need a 64-bit result.

Function Signature

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

Rules

  • A self-loop is a cycle of one vertex, and its sum is that vertex's index. A self-loop on vertex 0 is a cycle with sum 0 , which is a valid answer and different from -1 .

Constraints

  • n == len(edges)
  • 1 <= n <= 10^5
  • -1 <= edges[i] < n
  • Self-loops ( edges[i] == i ) are allowed.

Examples

Input: edges = [2,0,1,4,5,3,4]
Output: 12

Vertices 0, 2 and 1 form the cycle 0 -> 2 -> 1 -> 0 with sum 3. Vertices 3, 4 and 5 form the cycle 3 -> 4 -> 5 -> 3 with sum 12. Vertex 6 only leads into the second cycle and is not counted. The larger sum is 12.

Input: edges = [1,2,-1,2]
Output: -1

The walks 0 -> 1 -> 2 and 3 -> 2 both end at vertex 2, which has no outgoing edge, so the graph has no cycle.

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

Vertex 0 points to itself, so it is a one-vertex cycle with sum 0. Vertex 1 leads into it and is not counted.