Reported August 2020
Bloombergdynamic programming

Directional Distance to Mines and Boundaries

Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Bloomberg OA. Under 2s to a working solution.
Founder's read

Mine cells return 0, and every open cell returns the sum of open cells it can see in four directions before hitting a mine or the edge. That's the Bloomberg question reported in August 2020, and it looks friendlier than it is. The grid can hit 10^6 cells, so rescanning four rays from every cell dies on a wide open board. The pattern is four directional sweeps that carry a running count, basically a small DP. Know that and the code is short. If you blank on the sweep logic during the live OA, StealthCoder runs invisibly on your screen as a safety net and gives you the structure in real time.

The problem

In mines, 1 is a mine and 0 is open. For every open cell, count open cells reachable in each cardinal direction before the first mine or boundary, and output the sum. Output 0 for mine cells.

Function
directionalDistances(mines: int[][]) → int[][]

Examples
Example 1
mines = [[0,0,1],[0,0,0]]
return = [[2,2,0],[3,3,2]]
Each value sums unobstructed open cells in the four directions.

Constraints
The rectangular grid has at most 10^6 cells.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is that each direction is independent, so you handle them one at a time and add into a single result grid. Sweep each row left to right with a counter. On a mine, reset the counter to 0. On an open cell, add the counter to the result, then increment it. Repeat right to left, then do the same down and up each column. The counter always equals the number of consecutive open cells just behind you, which is exactly what the problem asks for. Check it against the example: cell (1,1) gets 1 from the left, 1 from the up and 1 from the right, so 3. The common pitfall is counting the cell itself, or incrementing before adding, which is off by one everywhere. Another is forgetting mine cells must stay 0. Total work is four passes over the grid, so O(rows*cols) time. If the sweeps feel shaky under the clock, StealthCoder is the hedge that keeps you from freezing.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Directional Distance to Mines and Boundaries 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. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Bloomberg's OA.

Bloomberg reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Directional Distance to Mines and Boundaries FAQ

How hard is the Bloomberg directional distance problem really?+

Easy-medium. There's no clever data structure. The difficulty is realizing you shouldn't scan outward from every cell. Once you see four linear sweeps with a running counter, it's about twenty lines. Most mistakes come from off-by-one errors, not from the idea.

What's the trick to getting it under O(n^2) per cell?+

Precompute by sweeping. For each direction, keep a counter of consecutive open cells directly behind you. Reset it to 0 on a mine. Add it to the cell's result before incrementing. Four passes cover left, right, up and down, and the total stays linear in the number of cells.

Does the cell itself count in the sum?+

No. Check the example: cell (0,0) in [[0,0,1],[0,0,0]] returns 2, which is one open cell below and one open cell to the right. Only other open cells reached before a mine or boundary count. The cell itself never adds to its own total.

What should mine cells output and how do I handle them?+

Mine cells output 0, and they act as walls. When your sweep hits a mine, reset the counter to 0 and don't add anything to that cell. The cell right after a mine then starts from 0 and sees nothing behind it in that direction.

How do I prepare for this in 48 hours?+

Write it once from scratch on a small grid and trace the example by hand. Then test edge cases: a single row, a single column, an all-mine grid, and an all-open grid. Those four catch nearly every off-by-one bug. Keep the code to four simple loops.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Bloomberg.

OA at Bloomberg?
Invisible during screen share
Get it