Count the Distinct Fastest Delivery Routes to a Customer

Last updated: November 29, 2026

Quick Overview

Given a weighted undirected road network, count how many different routes from the station (node 0) to the customer (node n - 1) achieve the minimum travel time. Return the count modulo 1,000,000,007.

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

0

0

0 solved


Given a weighted undirected road network, count how many different routes from the station (node 0) to the customer (node n - 1) achieve the minimum travel time. Return the count modulo 1,000,000,007.

Route planning at Amazon often needs to know whether the fastest route is unique or has alternatives for resilience. The interviewer checks that you extend Dijkstra to propagate path counts and handle ties correctly.

What the Interviewer Expects
  • Run Dijkstra from the station
  • Keep a ways array alongside distances
  • Reset ways on a strictly shorter distance, add on an equal one
  • Skip stale heap entries before propagating
  • Apply the modulus on every addition
Key Topics to Cover
Dijkstra
path counting
dynamic programming on DAG
modular arithmetic
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
  • Why does processing in Dijkstra order guarantee counts are final when a node is popped?
  • How would you count routes within 5 percent of the fastest time?
  • How would you return one fastest route along with the count?
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

Input: n intersections, undirected roads (u, v, t) with positive travel times. Output: the number of distinct shortest-time routes from 0 to n - 1 modulo 1e9+7. Two routes differ if their sequence...

High-Level Design

Shortest paths with positive weights form a DAG ordered by distance. Dijkstra pops nodes in distance order, so when a node is popped all its shortest-path predecessors have already contributed. Mainta...


Submit Your Answer
Markdown supported