Shortest Path to Food with Directions
Updated
Problem statement
You are given a rectangular grid containing exactly one starting cell * and exactly one food cell #. Open cells contain O, and blocked cells contain X.
From an open or starting cell, you may move one step up, down, left, or right without leaving the grid or entering a blocked cell. Return a shortest direction string that moves from * to #. Use the characters U, D, L, and R.
If several shortest strings exist, return the lexicographically smallest one. If the food cannot be reached, return an empty string.
Function
shortestFoodPath(grid: String[]) → StringExamples
Example 1
grid = ["XXXXXX","X*OOOX","XOX#OX","XOOOOX","XXXXXX"]return = "RRD"Move right twice and then down. No route uses fewer than three steps.
Example 2
grid = ["X*O","OO#"]return = "DR"Both DR and RD are shortest. DR is lexicographically smaller.
Example 3
grid = ["*X#"]return = ""The blocked middle cell separates the start from the food.
Constraints
1 <= grid.length <= 200.1 <= grid[i].length <= 200, and every row has the same length.- Every cell is one of
*,#,O, orX. - The grid contains exactly one
*and exactly one#.
More Zip Phone Screen questions
- Maximum Survival Time in a Graph PursuitPHONE SCREEN · Seen Sep 2026
- Find All Zip Rummy MeldsPHONE SCREEN · ONSITE INTERVIEW · Seen Apr 2026
- Shortest Path Around Moving ParadesPHONE SCREEN · Seen Mar 2026
- Furthest Building with Sandbags and RopesPHONE SCREEN · Seen Feb 2026
- Phrase Covering the Most WordsPHONE SCREEN · Seen Feb 2026
- Translate Values in a Nested MapPHONE SCREEN · Seen Feb 2026
- Maximum Nonadjacent Sum in a General TreePHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2025
- Shortest Distance in a GraphPHONE SCREEN · Seen Nov 2025
- Open the Lock and Return a Shortest PathPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2025
- Winning Draws for a Mahjong HandPHONE SCREEN · Seen Sep 2025
Similar questions at other companies
- Shortest Travel Time in a Grid with Traffic LightsWaymo · PHONE SCREENWaymo interview questions
- Shortest Path in a Blocked GridSpotify · PHONE SCREEN
- Shortest Path in a Binary MatrixMeta · PHONE SCREENMeta interview questions
- Shortest Grid Path With One Wall BreakAmazon · PHONE SCREEN
- Place a Service Table Near All CustomersGoogle · PHONE SCREENGoogle interview questions
- Shortest Path in a Grid with Obstacles EliminationMercor · PHONE SCREENMercor interview questions
- Shortest Path in a Grid with Obstacles EliminationBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Fire Station Response BillLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Minimum MovesHarness · OA
- Shortest Increasing Path to a TargetUber · ONSITE INTERVIEWUber interview questions