Not sure what to study next?

Get a personalised quant interview roadmap built around your upcoming interviews, strengths and weaknesses.

Takes a few minutes. Start with your target role and firms.

← Programming questions
Medium / PYTHON

Shortest Open Path across a Grid

graphs · breadth_first_search · grids 25 minute study target

Not completed

Challenge

A grid uses 0 for an open cell and 1 for a blocked cell. Return the minimum number of moves from start=[row,column] to goal. Each move goes one cell up, down, left or right; diagonal moves are forbidden. Return -1 if either endpoint is blocked or no path exists.

Input and output contract

A nonempty rectangular grid with at most 40,000 cells; both coordinates are in bounds. If start equals goal and the cell is open, return 0. A move count is an edge count, not a count of visited cells.

All tests follow this contract. You do not need to validate malformed input. Return the requested result from grid_shortest_path; printed output is not your answer. Standard-library tools are allowed. Implement your solution in Python 3.12.

Target complexity: O(rows × columns) time and auxiliary space

Return values are checked by type. Use the exact function name. Print output is not your answer. Tests have time and memory limits.

Public examples

Example 1

Arguments

[[[0, 1, 0], [0, 0, 0]], [0, 0], [0, 2]]

Expected return

4
Example 2

Arguments

[[[0]], [0, 0], [0, 0]]

Expected return

0
Example 3

Arguments

[[[0, 1], [1, 0]], [0, 0], [1, 1]]

Expected return

-1
Hint

Every move has equal cost, so process reachable cells in increasing distance with a queue.

The worked solution unlocks after completion, or with Premium access.

grid_shortest_path.pyPython 3.12

Drafts are saved to your account, not to shared browser storage.

Sign in to submit code and save progress.

Results

Your results will appear here.

Hidden tests reveal only their pass/fail status, never their inputs or expected outputs. Runtime is observed wall time—not an inferred complexity class.

Join our Newsletter and Receive the Latest Updates!

Main Links

Trade Courses Assessments Interview Prep Contact Us

Social Media

Facebook Instagram YouTube

Legal

Terms and Conditions Privacy Policy Privacy settings

Talent Acquisition

Looking to hire exceptional quant talent?
See how we can help.

© 2026 EverythingQuant. All Rights Reserved.