Find the shortest travel time between two train stations

Quick Overview

Given train stations joined by two-way tracks with positive travel times, return the shortest travel time between a source and a target station, or -1 when no route exists. It tests weighted shortest-path reasoning, adjacency lists and priority queues on graphs with up to 100,000 tracks.

Find the shortest travel time between two train stations

Company: Goldman Sachs

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given an integer `n`, the number of train stations, labeled from `0` to `n - 1`, and a list `connections` where each entry `[u, v, w]` is a **bidirectional** track between station `u` and station `v` with travel time `w`. Given two stations `source` and `target`, return the shortest total travel time from `source` to `target`. If no sequence of tracks connects the two stations, return `-1`. If `source == target`, the answer is `0`. ### Function Signature ```python def shortest_travel_time(n: int, connections: list[list[int]], source: int, target: int) -> int: ``` ### Constraints - `1 <= n <= 10^4` - `0 <= len(connections) <= 10^5` - Each connection is `[u, v, w]` with `0 <= u, v < n` and `1 <= w <= 10^4` - `0 <= source, target < n` - Every track can be used in both directions. ### Examples ```text Input: n = 6, connections = [[0,1,4],[0,2,1],[2,1,2],[1,3,1],[2,3,5],[3,4,3],[4,5,1]], source = 0, target = 5 Output: 8 ``` The route `0 -> 2 -> 1 -> 3 -> 4 -> 5` takes 1 + 2 + 1 + 3 + 1 = 8. Using the direct track from 0 to 1 instead costs 4 + 1 + 3 + 1 = 9, and going `0 -> 2 -> 3` costs 1 + 5 + 3 + 1 = 10. ```text Input: n = 4, connections = [[0,1,1],[1,2,2]], source = 0, target = 3 Output: -1 ``` Station 3 has no tracks at all, so no route from station 0 reaches it. ```text Input: n = 3, connections = [[0,1,5]], source = 2, target = 2 Output: 0 ``` The source and target are the same station, so the travel time is 0 even though station 2 has no tracks.

Overview: Given train stations joined by two-way tracks with positive travel times, return the shortest travel time between a source and a target station, or -1 when no route exists. It tests weighted shortest-path reasoning, adjacency lists and priority queues on graphs with up to 100,000 tracks.

Goldman Sachs logo
Goldman Sachs
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given an integer n, the number of train stations, labeled from 0 to n - 1, and a list connections where each entry [u, v, w] is a bidirectional track between station u and station v with travel time w.

Given two stations source and target, return the shortest total travel time from source to target. If no sequence of tracks connects the two stations, return -1. If source == target, the answer is 0.

Function Signature

def shortest_travel_time(n: int, connections: list[list[int]], source: int, target: int) -> int:

Constraints

  • 1 <= n <= 10^4
  • 0 <= len(connections) <= 10^5
  • Each connection is [u, v, w] with 0 <= u, v < n and 1 <= w <= 10^4
  • 0 <= source, target < n
  • Every track can be used in both directions.

Examples

Input: n = 6, connections = [[0,1,4],[0,2,1],[2,1,2],[1,3,1],[2,3,5],[3,4,3],[4,5,1]], source = 0, target = 5
Output: 8

The route 0 -> 2 -> 1 -> 3 -> 4 -> 5 takes 1 + 2 + 1 + 3 + 1 = 8. Using the direct track from 0 to 1 instead costs 4 + 1 + 3 + 1 = 9, and going 0 -> 2 -> 3 costs 1 + 5 + 3 + 1 = 10.

Input: n = 4, connections = [[0,1,1],[1,2,2]], source = 0, target = 3
Output: -1

Station 3 has no tracks at all, so no route from station 0 reaches it.

Input: n = 3, connections = [[0,1,5]], source = 2, target = 2
Output: 0

The source and target are the same station, so the travel time is 0 even though station 2 has no tracks.