PracHub
QuestionsPremiumLearningGuidesInterview PrepNEWCoaches
|Home/Coding & Algorithms/Uber

Find cheapest flight with at most K stops

Last updated: Mar 29, 2026

Quick Overview

This question evaluates graph algorithm proficiency and problem-modeling skills, specifically working with directed weighted graphs to optimize path cost under a constraint on intermediate stops.

  • hard
  • Uber
  • Coding & Algorithms
  • Software Engineer

Find cheapest flight with at most K stops

Company: Uber

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

## Problem You are given a directed weighted graph representing flights between cities. ### Inputs - An integer `n`: number of cities, labeled `0..n-1`. - A list `flights`, where each element is `[from, to, price]` meaning there is a flight from city `from` to city `to` that costs `price`. - Two integers `src` and `dst`: the start and destination cities. - An integer `K`: the maximum number of **stops** allowed, where a stop is an intermediate city between `src` and `dst`. - Equivalently, you may take at most `K+1` flights (edges). ### Output Return the minimum total cost to travel from `src` to `dst` using **at most `K` stops**. If it is not possible, return `-1`. ### Notes / Clarifications - You may assume all `price` values are non-negative integers. - The route must follow the directed edges as given in `flights`. ### Example - `n = 4` - `flights = [[0,1,100],[1,2,100],[2,3,100],[0,3,500]]` - `src = 0, dst = 3, K = 1` The cheapest valid route is `0 -> 1 -> 2 -> 3` is **not** allowed (2 stops), so the answer is `500` via `0 -> 3`.

Quick Answer: This question evaluates graph algorithm proficiency and problem-modeling skills, specifically working with directed weighted graphs to optimize path cost under a constraint on intermediate stops.

Related Interview Questions

  • Implement stream queries and bounded-difference subarrays - Uber (medium)
  • Implement Minesweeper and Word Search - Uber (medium)
  • Implement Store Autocomplete - Uber (medium)
  • Implement Cache Eviction And Seat Assignment - Uber (medium)
  • Simulate a Rank-Based Tournament - Uber (medium)
Uber logo
Uber
Dec 25, 2025, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
6
0

Problem

You are given a directed weighted graph representing flights between cities.

Inputs

  • An integer n : number of cities, labeled 0..n-1 .
  • A list flights , where each element is [from, to, price] meaning there is a flight from city from to city to that costs price .
  • Two integers src and dst : the start and destination cities.
  • An integer K : the maximum number of stops allowed, where a stop is an intermediate city between src and dst .
    • Equivalently, you may take at most K+1 flights (edges).

Output

Return the minimum total cost to travel from src to dst using at most K stops. If it is not possible, return -1.

Notes / Clarifications

  • You may assume all price values are non-negative integers.
  • The route must follow the directed edges as given in flights .

Example

  • n = 4
  • flights = [[0,1,100],[1,2,100],[2,3,100],[0,3,500]]
  • src = 0, dst = 3, K = 1

The cheapest valid route is 0 -> 1 -> 2 -> 3 is not allowed (2 stops), so the answer is 500 via 0 -> 3.

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Uber•More Software Engineer•Uber Software Engineer•Uber Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.