BFS on grid

Last updated: November 23, 2025

Quick Overview

Given a 2D grid of 0s and 1s, where 0 represents an empty cell and 1 represents an obstacle, implement a Breadth-First Search (BFS) algorithm to find the shortest path from the top-left corner to the bottom-right corner of the grid. Return the length of the path, or -1 if no path exists.

MongoDB
Coding & Algorithms
Software Engineer
MongoDB
November 23, 2025
Software Engineer
Phone Screen
Coding & Algorithms
Medium

25

11

4,809 solved


Given a 2D grid of 0s and 1s, where 0 represents an empty cell and 1 represents an obstacle, implement a Breadth-First Search (BFS) algorithm to find the shortest path from the top-left corner to the bottom-right corner of the grid. Return the length of the path, or -1 if no path exists.

MongoDB uses this problem in the Phone Screen to evaluate your algorithmic thinking. They expect you to discuss multiple approaches, analyze trade-offs between them, and implement the optimal solution with clean, readable code.

What the Interviewer Expects
  • Recognize the underlying problem pattern (sliding window, two pointers, BFS/DFS, etc.)
  • Discuss multiple approaches and trade-offs before coding
  • Implement an optimal solution with clean, production-quality code
  • Handle all edge cases including boundary conditions and invalid input
  • Optimize both time and space complexity with clear justification
  • Test your solution systematically with well-chosen examples
Key Topics to Cover
Graph algorithms and traversal
Tree structures and recursion
Dynamic programming and memoization
Sorting and searching
Edge cases and input validation
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • What if the input doesn't fit in memory?
  • How would you parallelize this solution?
  • What is the worst-case input for your solution?
  • How would your solution change if the input was sorted?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Problem Analysis

This problem can be efficiently solved using the Breadth-First Search (BFS) algorithm because it explores all possible paths from the starting point (top-left corner) level by level, ensuring that we ...

Approach
  1. Initialization: Start by checking if the starting point (0,0) or the destination (n-1,m-1) is an obstacle (1). If either is blocked, return -1 immediately.
  2. Queue setup: Use a queue to fa...

Submit Your Answer
Markdown supported

Related Questions