Shortest driving tour of resorts using maps APIs
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Scale AI's applied engineering round, last asked May 2025. It tests apis, traveling salesman and dynamic programming.
A practical round in a small Python project. Starting from a home address, a traveller wants to visit every resort on a list once and drive back home, spending as little time in the car as possible. Build the pipeline: look up each place with a places API, collect the driving time between every pair of places with a routes API, then compute the best tour.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
resolve_place(query: str) -> tuple[float, float]— wraps the places API and returns a latitude and longitude; raises when nothing matches. -
driving_seconds(origin, destination) -> int— wraps the routes API for one ordered pair; it can time out, fail or be rate limited. -
build_matrix(places: list[str]) -> list[list[int]]— resolves every place and fills the pairwise driving-time matrix, with retries, backoff and caching. -
best_tour(matrix: list[list[int]]) -> tuple[int, list[int]]— index 0 is home; returns the total time and the visiting order, starting and ending at home.
Example
places = ["home", "A", "B", "C"]
matrix = [[ 0, 30, 50, 40],
[30, 0, 20, 45],
[50, 20, 0, 25],
[40, 45, 25, 0]] # minutes
best_tour(matrix) -> (115, [0, 1, 2, 3, 0]) # 30 + 20 + 25 + 40; the reverse order also costs 115
Constraints
- A handful of resorts in the base task; be ready to talk about 15 to 20.
- Driving times need not be symmetric (one-way streets, traffic).
- The APIs are slow and metered: don't request the same pair twice.
Clarifying questions
- Is the matrix symmetric, or must both directions be fetched?
- What should happen when a place can't be resolved or a pair has no route?
- What are the API's rate limits and quotas, and is a batch matrix endpoint available?
- Must the tour return home, or may it end at the last resort?
- Should the answer use live traffic or a fixed departure time?
What a strong answer covers
- Clean API wrappers with timeouts, retries with backoff and jitter on 429 and 5xx, and no retry on bad input.
- Caching resolved places and pair times so a rerun costs nothing.
- Brute force over permutations,
O(n!), for a few stops, then the Held-Karp bitmask dynamic program,O(2^n · n^2)time andO(2^n · n)memory. - Rebuilding the route from the DP's parent pointers, not only the cost.
- Tests with a fake routes client: failures, a missing route, and a small matrix with a known best tour.
Follow-ups
- 50 or more stops: nearest-neighbour plus 2-opt, and how far from optimal it can land.
- Opening hours at each resort (time windows).
- Cut API calls: fetch the matrix in batches and skip pairs that can't be on a good tour.
Where it's asked
- Scale AI · Applied engineering · Last asked May 202560 min · Build a small working system
What it tests
apis · traveling salesman · dynamic programming
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
