Fastest Ride Route Using at Most K Toll Roads

Last updated: November 29, 2026

Quick Overview

Given a city road graph where each road has a travel time and may be a toll road, find the minimum travel time from pickup to drop-off using at most `k` toll roads, or return -1 if impossible.

Grab
Coding & Algorithms
Software Engineer
Grab
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Given a city road graph where each road has a travel time and may be a toll road, find the minimum travel time from pickup to drop-off using at most `k` toll roads, or return -1 if impossible.

Routing and ETA are central to GrabCar and GrabFood, and passengers in cities like Singapore and Jakarta often prefer to limit tolls such as ERP gantries. The interviewer checks that you extend Dijkstra with a constraint dimension instead of hacking it with a plain visited set.

What the Interviewer Expects
  • Explain why plain Dijkstra with a single distance per node is wrong under a toll budget
  • Expand state to (node, tolls used)
  • Use a min-heap with lazy deletion of stale entries
  • Stop early when the destination is popped
  • State complexity in terms of V, E and k
Key Topics to Cover
Dijkstra
State-space expansion
Constrained shortest path
Priority queue
Routing
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • What if each toll has a price and the constraint is a total budget instead of a count?
  • How would you return the path itself?
  • How do production routing engines speed this up (contraction hierarchies, A*)?
  • What if travel times change by time of day?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Undirected roads (u, v, time, is_toll) with non-negative times, nodes 0..n-1, source, destination and toll budget k. Return the minimum time using at most k toll roads, else -1. A fast path wi...

High-Level Design

Run Dijkstra over the product graph whose states are (node, tolls_used) with tolls_used in 0..k. Taking a toll road moves from layer t to t+1; free roads stay in the layer. Because all weigh...


Submit Your Answer
Markdown supported

Related Questions