Reported November 2023
Bloombergmatrix

Maximum Visibility in a Forest Grid

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

The mistake that sinks most first attempts at Bloomberg's Maximum Visibility in a Forest Grid, reported in November 2023, is scanning outward from every open cell. With up to 10^6 cells, that blows up fast on a mostly open grid. The task is simple to state: find the open cell that sees the most other open cells in four directions before a tree or the edge stops it. Ties go to row-major order, and an all-tree grid returns [-1,-1]. The hint says BFS, but the real win is precomputation. If you blank during the live OA, StealthCoder is the safety net running invisibly on your screen.

The problem

Each character in forest is open land _ or a tree X. For an open cell, visibility is the number of other open cells visible in the four cardinal directions before a tree or grid boundary blocks the view.
Return [row,column] for the open cell with maximum visibility. Break ties by row-major order. Return [-1,-1] if every cell is a tree.

Function
maximumVisibilityCell(forest: String[]) → int[]

Examples
Example 1
forest = ["___X","_X__","__X_"]
return = [0,0]
The top-left cell sees four other open cells, the maximum.

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

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is that visibility along a row or column is shared by every cell in the same unbroken segment. So you don't need BFS or a per-cell walk. Do four sweeps, left, right, up, down, and store how many consecutive open cells sit in each direction. Visibility for a cell is the sum of the four counts, and each count excludes the cell itself. That's O(rows*cols) time. The common pitfall is counting the cell itself, or counting it twice because horizontal and vertical runs both include it. Another is the tie-break. Scan in row-major order and only update on a strictly greater value, so the first cell wins. Initialize the best to -1 so a grid of all trees returns [-1,-1]. Check Example 1: the cell at [0,0] sees two to the right and two below, which is four. If the sweeps feel fiddly under pressure, StealthCoder can supply the structure live.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Maximum Visibility in a Forest Grid 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 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. 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.

Maximum Visibility in a Forest Grid FAQ

What's the trick to Maximum Visibility in a Forest Grid?+

Don't walk outward from each cell. Precompute consecutive open-cell counts in each of the four directions with linear sweeps, then sum them per cell. That gives O(rows*cols) instead of something that can degrade badly on an open grid with 10^6 cells.

Do I actually need BFS here?+

No. BFS is the hinted pattern, but the visibility rule only goes in straight lines, so there's no shortest path or flood fill. Directional prefix-style counting is cleaner and faster. Use BFS only if you misread the problem as connectivity.

How do I handle ties and the all-trees case?+

Iterate in row-major order and update the answer only when visibility is strictly greater than the current best. Start the best at -1 with the answer [-1,-1]. If every cell is a tree, nothing ever updates it and you return that default.

What's the most common bug on this problem?+

Off-by-one on the cell itself. Visibility counts other open cells, so the cell shouldn't be included. If your run counts include the current cell, subtract it properly or define the counts as cells strictly beyond it. Test it on Example 1, which should give 4.

How do I prepare for this in 48 hours?+

Practice grid problems that use directional sweeps and run-length counts. Write the four-pass solution once from scratch, then test a single row, a single column, an all-tree grid, and a fully open grid. Those edge cases cover most failures.

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