Replacing Val

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

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

Two Sigma's March 2024 OA asks you to implement a flood-fill algorithm. You're given a 2D array, a starting position, and a replacement value. The task: replace the value at the starting position and recursively spread that replacement to all adjacent cells (up, down, left, right) that share the original value. It's a classic pattern disguised as a simple description. StealthCoder will read the problem and serve up the recursive or BFS solution if you blank on the approach.

The problem

\ \ Sup 👋!\ \ This time the agency will give you:\ An int 2D arr
\ A position within the input 2D arr
\ A so called replacing val
\
\ \ What they want you to do: \ \ Replace the val at the starting position with the provided replacing val. Repeat this process for all positions connected to the starting position via sides with the same val as the starting position.\

Reported by candidates. Source: FastPrep

Pattern and pitfall

This is a textbook flood-fill (also called color-fill or paint-bucket in image editors). The trick is recognizing that 'connected via sides with the same val' means you need to visit every cell reachable from the start without crossing a boundary of different values. Most candidates reach for recursion first, which works but can hit stack limits on large grids. BFS with a queue is safer and predictable. The gotcha: mark cells as visited or changed immediately to avoid revisiting them in the same traversal. If you freeze during the OA, StealthCoder will surface the iterative BFS pattern so you don't lose points on implementation.

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 Replacing Val 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 flood fill. If you have time before the OA, drill that.

⏵ The honest play

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

Two Sigma 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.

Replacing Val FAQ

Is this just DFS or BFS?+

Both work. DFS (recursion) is intuitive but risks stack overflow on massive grids. BFS with a queue is safer and what most interviewers prefer. Either passes Two Sigma if it's correct and doesn't revisit cells.

What's the gotcha Two Sigma is testing?+

Infinite loops from revisiting cells. Mark cells as visited the moment you add them to your queue or recursion stack, not when you process them. That's where most candidates slip.

Does the starting position always exist in the grid?+

The problem doesn't say it's invalid, so assume it's valid. But a defensive check (bounds, null guards) won't hurt and signals careful thinking to the interviewer.

What if the replacement value is the same as the original?+

You'll still traverse the entire connected region, but no visible change occurs. Optimize: if original == replacement, return early. Two Sigma might test this edge case.

Time and space limits for this problem?+

Worst case, you touch every cell once (O(m*n) time). Space is O(m*n) for the queue or recursion stack if the entire grid is one color. That's acceptable for standard grid sizes.

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

OA at Two Sigma?
Invisible during screen share
Get it