Shortest Bridge to Connect Two Islands

Last updated: June 29, 2026

Quick Overview

Given a 2D grid representing water and land, find the shortest bridge (minimum number of 0s) that connects two islands (1s).

Tesla
Coding & Algorithms
Software Engineer
Tesla
June 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Easy

6

0

46 solved


Given a 2D grid representing water and land, find the shortest bridge (minimum number of 0s) that connects two islands (1s).

This question is typically asked in the onsite coding round to evaluate a candidate's problem-solving and algorithmic skills. Interviewers are looking for the ability to translate a real-world problem into a computational solution, as well as proficiency in grid-based algorithms. Candidates are also assessed on their coding style, efficiency, and clarity of thought during the problem-solving process.

What the Interviewer Expects
  • Identify the two islands in the grid.
  • Use a breadth-first search (BFS) or depth-first search (DFS) approach to explore the grid.
  • Calculate the minimum distance to connect the two islands.
  • Discuss the time and space complexity of the solution.
  • Write clean, efficient, and well-commented code.
Key Topics to Cover
Graph Traversal
Breadth-First Search
Depth-First Search
Grid-based Algorithms
Complexity Analysis
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
  • How would you optimize this solution for larger grids?
  • Can you explain the difference between BFS and DFS in this context?
  • What edge cases would you consider for this problem?
Sharpen Your Skills on Codemia

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

Practice DSA Problems
Sample Answer
Approach

To solve the problem of finding the shortest bridge between two islands, we can use a breadth-first search (BFS) approach. First, we need to identify the two islands in the grid. We can do this by per...

Algorithm
  1. Identify the first island: Use DFS to traverse the grid and mark all cells of the first island with a unique identifier.
  2. Perform BFS: Start from the marked cells of the first island and ...

Submit Your Answer
Markdown supported

Related Questions