Shortest Bridge
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The naive move on Shortest Bridge is to grab any 1 and start a BFS, and that's the edge case that wrecks you. Bloomberg reported this one in November 2020, and the grid has exactly two islands, so you have to separate them before you measure anything. If you BFS from a random 1 without marking the first island, you'll just wander across island A and report a distance of zero. The pattern is DFS plus multi-source BFS on a matrix. If you blank mid-assessment, StealthCoder runs invisibly on your desktop and gives you the working solution as a safety net.
The problem
grid contains exactly two four-directionally connected islands of 1s. Return the minimum number of 0 cells that must be changed to 1 to connect the islands. Function shortestBridge(grid: int[][]) → int Examples Example 1 grid = [[0,1],[1,0]] return = 1 Flipping either remaining zero joins the islands. Constraints 2 <= grid.length, grid[0].length <= 100. The grid contains exactly two islands.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Here's the trick. Step one: scan for the first 1 and DFS the whole island, marking every cell visited (flip to 2 or use a seen set) and pushing each cell into a queue. Step two: run BFS outward from that entire queue at once, level by level. Each level is one flipped 0. The first time you touch a 1 that isn't marked, return the level count. The pitfall is starting BFS from a single cell instead of the whole island, which gives a wrong, too-large answer. Another one is forgetting to mark visited zeros, so the queue blows up on a 100 by 100 grid. Check the four-direction bounds carefully. On the example [[0,1],[1,0]], island one is (0,1), BFS reaches a zero at level one, then hits the other island, so the answer is 1. If the live OA freezes you, StealthCoder is the hedge that hands you this structure.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Shortest Bridge cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as shortest bridge. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Shortest Bridge FAQ
What's the trick in Shortest Bridge?+
Split it into two phases. DFS one island to collect all its cells and mark them. Then run a multi-source BFS from every cell of that island at once. The BFS depth when you first reach the other island's 1 is your answer.
Why does BFS from a single cell fail?+
An island can be large and irregular. The shortest bridge might leave from any edge cell, not the one you picked. Seeding the queue with the entire island makes BFS explore all possible starting points in parallel, so the first hit is guaranteed shortest.
How hard is this one really for the Bloomberg OA?+
It's medium. Neither DFS nor BFS is hard alone. The difficulty is chaining them cleanly and handling visited states without bugs. If you've written flood fill and standard grid BFS, you can finish this in one sitting.
What's the time complexity I should state?+
O(n * m) time and space, where the grid is n by m. Each cell is visited at most once by the DFS and at most once by the BFS. With sizes up to 100 by 100, that's well within limits.
How do I prepare for this in 48 hours?+
Write flood fill DFS and a grid BFS with a direction array from memory. Then combine them: DFS fills the queue, BFS expands by levels. Test on the 2 by 2 example and one grid where the islands are far apart.