Skip to main content
All Questions

Return the k nearest restaurants to a starting cell in a grid with walls.

Unlock detailed company stats for this questionUpgrade

You are given a 2D grid representing a map. Each cell is ' ' (empty, traversable), '-' (a wall that cannot be traversed) or 'A' to 'Z' (a restaurant). You are also given a starting position (row, col) and an integer k. Return the top k nearest restaurants from the starting position based on the minimum number of steps required to reach them. Movement is in 4 directions (up, down, left, right).

Output: a dictionary/map of at most k restaurants with their minimum step counts; if fewer than k are reachable, return all reachable ones.

Constraints: grid size m x n, 1 <= m, n, k >= 1, the starting position is within bounds, restaurants are labeled 'A'-'Z'.

Signature: nearest_restaurants(grid: List[List[str]], start_row: int, start_col: int, k: int) -> Dict[str, int].

Format · Live coding on HackerRank after a background discussion.

Community answers

No answers contributed by the community yet.

Related courses

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.

Course

System Design Interviews

Learn how to answer the latest system design questions across product, infrastructure, and AI domains. Features in-depth video examples, written breakdowns, and helpful reference patterns. Watch senior engineers and managers answer these questions in mock interview videos.