Fastest Apple Maps Route With Penalties for Changing Roads
Last updated: November 29, 2026
Quick Overview
Road segments are directed edges (u, v, minutes, road_name). A route pays an extra P minutes each time it moves from one road to a differently named road, modeling turns. Return the minimum total time from source to destination, or -1 if unreachable.
Apple
November 29, 20260
0
0 solved
Road segments are directed edges (u, v, minutes, road_name). A route pays an extra P minutes each time it moves from one road to a differently named road, modeling turns. Return the minimum total time from source to destination, or -1 if unreachable.
Routing is core to Apple Maps, and turn costs are why textbook Dijkstra on intersections is not enough. The interviewer checks that you expand the state to include how you arrived and that you keep Dijkstra's correctness under that expanded state.
What the Interviewer Expects
- Explain why node-only Dijkstra gives wrong answers with turn costs
- Define the state as (intersection, current road)
- Run Dijkstra on the expanded state space with a heap
- Stop when the destination is first popped in any state
- Analyze O(E log E) time on the expanded graph
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Left turns cost more than right turns; what geometry do you need?
- Some turns are banned at specific intersections.
- Precompute for fast queries across a city with contraction hierarchies.
- Travel times change with live traffic every minute.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: list of edges (u, v, minutes, road), source, target, penalty P >= 0. Output: minimum minutes, counting P for every consecutive pair of edges with different road names. The first edge ha...
High-Level Design
Two routes reaching the same intersection on different roads have different futures, because continuing straight is free for one and costs P for the other. So the Dijkstra state is (node, road), wit...