Find the Cell Reachable from Two Starts with the Smallest Total Distance

Quick Overview

In a directed graph where each cell has at most one outgoing edge, find the cell reachable from two starting cells that minimizes the sum of the two walk distances, breaking ties by the smallest index. It tests walking graphs that contain cycles, distance bookkeeping, and careful tie handling.

Find the Cell Reachable from Two Starts with the Smallest Total Distance

Company: Juspay

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a directed graph of `n` cells numbered `0` to `n - 1`, represented by an array `edges` of length `n`. Each cell has at most one outgoing edge: `edges[i]` is the cell that cell `i` points to, or `-1` if cell `i` has no outgoing edge. Self loops (`edges[i] == i`) are allowed. You are also given two starting cells `node1` and `node2`. A walk from a starting cell follows outgoing edges one step at a time. Each starting cell is at distance `0` from itself. A walk stops when it reaches a cell with no outgoing edge or when the next cell has already been visited by that same walk. Return the cell that is reachable from both `node1` and `node2` such that `distanceFromNode1 + distanceFromNode2` is minimized. If several cells share the minimum total distance, return the one with the smallest index. If no cell is reachable from both starts, return `-1`. The objective is the sum of the two distances. A related problem minimizes the larger of the two distances instead, and Example 1 shows that the two rules can pick different cells. ### Function Signature ```python def nearest_meeting_cell(edges: list[int], node1: int, node2: int) -> int: ``` ### Rules - The distance from a start to a cell is the number of edges its walk follows to first reach that cell. A start reaches itself at distance 0. - A cell qualifies only if both walks reach it. - Minimize `dist1[c] + dist2[c]` over qualifying cells `c`, and break ties by the smallest index `c`. - `node1` and `node2` may be the same cell. - Return `-1` when no cell qualifies. ### Constraints - `n == len(edges)` - `1 <= n <= 10^5` - `-1 <= edges[i] < n` - `0 <= node1, node2 < n` - Self loops (`edges[i] == i`) are allowed. ### Examples ```text Input: edges = [1,2,0,4,2], node1 = 0, node2 = 3 Output: 0 ``` The edges form the cycle `0 -> 1 -> 2 -> 0`, and the walk from 3 enters it through `3 -> 4 -> 2`. Cells 0, 1 and 2 are reachable from both starts with sums 0 + 3 = 3, 1 + 4 = 5 and 2 + 2 = 4, so cell 0 wins (minimizing the larger distance would pick cell 2, whose larger distance is 2). ```text Input: edges = [1,2,3,4,0], node1 = 0, node2 = 2 Output: 2 ``` All five cells lie on the cycle `0 -> 1 -> 2 -> 3 -> 4 -> 0`. The distances from 0 to cells 0 through 4 are 0, 1, 2, 3, 4 and from 2 they are 3, 4, 0, 1, 2, so the sums are 3, 5, 2, 4, 6 and cell 2 has the minimum. ```text Input: edges = [1,0], node1 = 0, node2 = 1 Output: 0 ``` Cell 0 has distances 0 and 1, and cell 1 has distances 1 and 0. Both sums are 1, so the smaller index, 0, is returned.

Overview: In a directed graph where each cell has at most one outgoing edge, find the cell reachable from two starting cells that minimizes the sum of the two walk distances, breaking ties by the smallest index. It tests walking graphs that contain cycles, distance bookkeeping, and careful tie handling.

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

You are given a directed graph of n cells numbered 0 to n - 1, represented by an array edges of length n. Each cell has at most one outgoing edge: edges[i] is the cell that cell i points to, or -1 if cell i has no outgoing edge. Self loops (edges[i] == i) are allowed.

You are also given two starting cells node1 and node2. A walk from a starting cell follows outgoing edges one step at a time. Each starting cell is at distance 0 from itself. A walk stops when it reaches a cell with no outgoing edge or when the next cell has already been visited by that same walk.

Return the cell that is reachable from both node1 and node2 such that distanceFromNode1 + distanceFromNode2 is minimized. If several cells share the minimum total distance, return the one with the smallest index. If no cell is reachable from both starts, return -1.

The objective is the sum of the two distances. A related problem minimizes the larger of the two distances instead, and Example 1 shows that the two rules can pick different cells.

Function Signature

def nearest_meeting_cell(edges: list[int], node1: int, node2: int) -> int:

Rules

  • The distance from a start to a cell is the number of edges its walk follows to first reach that cell. A start reaches itself at distance 0.
  • A cell qualifies only if both walks reach it.
  • Minimize dist1[c] + dist2[c] over qualifying cells c , and break ties by the smallest index c .
  • node1 and node2 may be the same cell.
  • Return -1 when no cell qualifies.

Constraints

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

Examples

Input: edges = [1,2,0,4,2], node1 = 0, node2 = 3
Output: 0

The edges form the cycle 0 -> 1 -> 2 -> 0, and the walk from 3 enters it through 3 -> 4 -> 2. Cells 0, 1 and 2 are reachable from both starts with sums 0 + 3 = 3, 1 + 4 = 5 and 2 + 2 = 4, so cell 0 wins (minimizing the larger distance would pick cell 2, whose larger distance is 2).

Input: edges = [1,2,3,4,0], node1 = 0, node2 = 2
Output: 2

All five cells lie on the cycle 0 -> 1 -> 2 -> 3 -> 4 -> 0. The distances from 0 to cells 0 through 4 are 0, 1, 2, 3, 4 and from 2 they are 3, 4, 0, 1, 2, so the sums are 3, 5, 2, 4, 6 and cell 2 has the minimum.

Input: edges = [1,0], node1 = 0, node2 = 1
Output: 0

Cell 0 has distances 0 and 1, and cell 1 has distances 1 and 0. Both sums are 1, so the smaller index, 0, is returned.