Fastest Route That Stops at a Coffee Shop Then a Restaurant

Last updated: November 29, 2026

Quick Overview

Given a weighted road graph, a start node, a set of coffee shop nodes and a set of restaurant nodes, find the minimum travel time route that visits at least one coffee shop and then ends at a restaurant, returning the time and the node path.

Yelp
Coding & Algorithms
Software Engineer
Yelp
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Given a weighted road graph, a start node, a set of coffee shop nodes and a set of restaurant nodes, find the minimum travel time route that visits at least one coffee shop and then ends at a restaurant, returning the time and the node path.

Yelp collections and trip planning features chain stops together, and this question models "grab coffee, then dinner" as a constrained shortest path. The interviewer is looking for Dijkstra over an expanded state space (node, has coffee) rather than running Dijkstra from every coffee shop.

What the Interviewer Expects
  • Recognize that order constraints call for a product state graph
  • Run a single Dijkstra over (node, state) pairs
  • Stop at the first popped restaurant in the post coffee state
  • Reconstruct the path with a predecessor map over states
  • Explain why running Dijkstra from every coffee shop is slower
Key Topics to Cover
Dijkstra
state space search
shortest paths
path reconstruction
weighted graphs
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
  • Extend to an ordered list of m categories (coffee, park, dinner). What is the state?
  • What if the stops can be visited in any order?
  • How would you add dwell time at the coffee shop?
  • How would you return the top 3 routes with different coffee shops?
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 graph with n nodes and non negative edge weights (minutes). A valid route starts at src, passes through some coffee node, and ends at a restaurant reached after that coffee node. Nodes ma...

High-Level Design

Double the graph. State (v, 0) means at v without coffee yet, (v, 1) means coffee done. Moving along edge u to v keeps the state, except entering a coffee node flips 0 to 1. The target is any `(r,...


Submit Your Answer
Markdown supported

Related Questions