This is a premium problem. We're working on making it available for free soon.
Explore Free ProblemsUse these hints if you're stuck. Try solving on your own first.
Find the distance from s to all nodes.
You can use Dijkstra to find them.
Find the minimum distance between marked nodes.
Solutions for this premium problem will be available for free soon.
Browse Free ProblemsWatch expert explanations and walkthroughs
Jot down your thoughts, approach, and key learnings
Yes, the problem is fundamentally a graph traversal problem. It requires modeling nodes and edges as a graph and applying shortest path techniques to determine the nearest marked node.
Problems involving shortest path algorithms like Dijkstra’s are common in FAANG interviews. Variants of this problem test a candidate’s understanding of graphs, priority queues, and efficient pathfinding.
A priority queue (min-heap) is the most suitable data structure for this problem. It allows efficient retrieval of the node with the smallest current distance during the shortest path computation.
The optimal approach is to use Dijkstra’s algorithm starting from the given source node. By using a priority queue, the algorithm always explores the node with the smallest distance first, ensuring the closest marked node is found efficiently.