Generate Matrix B

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

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

You've got a matrix generation problem from Arista Networks reported in September, and it's a BFS trap. The problem looks like it wants you to simulate some kind of growth or propagation outward from a starting point. Candidates blank on these because they try to overthink the traversal order. StealthCoder will spot the pattern the moment you see the problem statement, but understanding the mechanics now means you won't need it.

Pattern and pitfall

Generate Matrix B is a classic BFS problem disguised as a matrix construction task. You're likely given a source matrix and need to build a new matrix where each cell contains the distance to the nearest source cell, or you're propagating values outward in layers. The trick is recognizing that BFS from all source cells simultaneously is faster than running single-source BFS repeatedly. You initialize a queue with all source positions at once, then expand level by level. Common pitfall: forgetting to mark visited cells during the push, leading to cycles or duplicate processing. The visited array or a distance matrix itself prevents re-queuing. StealthCoder as your safety net means if you blank on queue mechanics during the live OA, you have real-time verification of your approach before you submit.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Generate Matrix B 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 passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as as far from land as possible. If you have time before the OA, drill that.

⏵ The honest play

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

Arista Networks reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Generate Matrix B FAQ

Is this just multi-source BFS?+

Yes. Start by enqueuing all sources at distance 0, then expand outward one layer at a time. The distance or value in the new matrix represents steps from any source. Most solutions use a queue and a visited set or overwrite the input matrix as you traverse.

What's the time complexity I should target?+

O(rows * cols) because each cell is enqueued and dequeued exactly once. Space is also O(rows * cols) for the queue in the worst case. If Arista cares about constant factors, avoid redundant re-visits by marking cells as visited before pushing, not after.

Do I need to build a new matrix or modify in place?+

The problem name suggests building a new matrix B, so assume you're returning a fresh 2D array. Modifying in place works for BFS, but check the function signature. If input is source and output is distances, you're building a distance matrix.

How do I avoid infinite loops in BFS?+

Use a visited set or mark cells as visited before enqueuing them, not after dequeuing. Once a cell is visited, never enqueue it again. This is critical in grid BFS to prevent cycles and redundant exploration.

Is there a trick to the direction vectors?+

Probably 4-directional (up, down, left, right). Some variants allow 8-directional or even custom movement. Read the problem carefully. The BFS logic doesn't change, only the number of neighbors you check per cell.

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

OA at Arista Networks?
Invisible during screen share
Get it