Choose the Warehouse Site Reaching the Most Cities Within a Drive Limit

Last updated: November 29, 2026

Quick Overview

Given candidate cities connected by weighted roads and a maximum drive distance D, pick the city from which the most other cities are reachable within distance D. Break ties by the smallest city index.

Amazon
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Given candidate cities connected by weighted roads and a maximum drive distance D, pick the city from which the most other cities are reachable within distance D. Break ties by the smallest city index.

Site selection for new Amazon delivery stations depends on how many demand centers a site can serve within a drive window. The interviewer wants all-pairs shortest paths, usually Floyd-Warshall for small n, and a clear tie-break.

What the Interviewer Expects
  • Recognize the need for all-pairs shortest distances
  • Implement Floyd-Warshall with the k loop outermost
  • Count reachable cities per source excluding itself
  • Apply the tie-break explicitly
  • Compare with running Dijkstra from every node for sparse graphs
Key Topics to Cover
Floyd-Warshall
all-pairs shortest paths
dynamic programming
graph coverage
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
  • When does n runs of Dijkstra beat Floyd-Warshall?
  • How would you choose two sites that together cover the most cities?
  • How would you handle roads whose time changes by hour 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

Input: n cities (n up to a few hundred), undirected roads (u, v, w) with positive lengths, and D. Output: the index of the city with the highest count of other cities at shortest distance <= D, ...

High-Level Design

Floyd-Warshall computes dist[i][j] as the shortest distance using only intermediate nodes 0..k after iteration k. Initialize with road lengths and zeros on the diagonal, then relax `dist[i][j] = min...


Submit Your Answer
Markdown supported