PracHub
QuestionsPremiumLearningGuidesInterview PrepNEWCoaches
|Home/Coding & Algorithms/Amazon

Find shortest path in a grid with obstacles

Last updated: Mar 29, 2026

Quick Overview

This question evaluates understanding of pathfinding in grid-based environments, graph traversal concepts, and algorithmic complexity when navigating obstacles and boundary constraints in an unweighted 2D search space.

  • medium
  • Amazon
  • Coding & Algorithms
  • Machine Learning Engineer

Find shortest path in a grid with obstacles

Company: Amazon

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

You are given a 2D grid of size `m x n` representing a maze. Each cell in the grid is either empty (0) or blocked (1). You are also given two coordinates: - `start = (sx, sy)` - `end = (ex, ey)` You can move from a cell to its 4-directional neighbors (up, down, left, right), but you **cannot** move into or through blocked cells, and you must remain inside the grid. Return the length of the shortest path (in number of steps) from `start` to `end`. If there is no valid path, return `-1`. Assumptions and details: - `0 <= sx, ex < m` - `0 <= sy, ey < n` - `grid[sx][sy] == 0` and `grid[ex][ey] == 0` - `1 <= m, n <= 10^3` - Time complexity should be efficient for the upper bounds (you may consider BFS or DFS-based approaches). **Input format (for reference):** - `m, n` (integers) - `grid` as an `m x n` matrix of 0s and 1s - `start` coordinate - `end` coordinate **Output:** - An integer representing the length of the shortest path, or `-1` if unreachable.

Quick Answer: This question evaluates understanding of pathfinding in grid-based environments, graph traversal concepts, and algorithmic complexity when navigating obstacles and boundary constraints in an unweighted 2D search space.

Related Interview Questions

  • Count Connected Components in an Undirected Graph - Amazon (medium)
  • Find Unique Target-Sum Pairs - Amazon (easy)
  • Find Valid IP Addresses in Files - Amazon (medium)
  • Implement Optimal Bucket Batching - Amazon (hard)
  • Implement Cache and Rotate Matrix - Amazon (medium)
Amazon logo
Amazon
Dec 8, 2025, 6:37 PM
Machine Learning Engineer
Onsite
Coding & Algorithms
4
0

You are given a 2D grid of size m x n representing a maze. Each cell in the grid is either empty (0) or blocked (1).

You are also given two coordinates:

  • start = (sx, sy)
  • end = (ex, ey)

You can move from a cell to its 4-directional neighbors (up, down, left, right), but you cannot move into or through blocked cells, and you must remain inside the grid.

Return the length of the shortest path (in number of steps) from start to end. If there is no valid path, return -1.

Assumptions and details:

  • 0 <= sx, ex < m
  • 0 <= sy, ey < n
  • grid[sx][sy] == 0 and grid[ex][ey] == 0
  • 1 <= m, n <= 10^3
  • Time complexity should be efficient for the upper bounds (you may consider BFS or DFS-based approaches).

Input format (for reference):

  • m, n (integers)
  • grid as an m x n matrix of 0s and 1s
  • start coordinate
  • end coordinate

Output:

  • An integer representing the length of the shortest path, or -1 if unreachable.

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Amazon•More Machine Learning Engineer•Amazon Machine Learning Engineer•Amazon Coding & Algorithms•Machine Learning Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.