2048 Move Simulator
Reported by candidates from Benchling's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The example in this Benchling question is the whole trap: [2,0,2,2] sliding left gives [4,2,0,0], not [2,4,0,0] and not [8,0,0,0]. Benchling reported this 2048 Move Simulator in February 2019, and it's pure simulation. No spawning, no scoring, just apply each move in moves to a square grid and return the result. Nothing here is hard algorithmically. It's easy to get one merge rule wrong under time pressure, though. If you blank on the direction handling, StealthCoder runs invisibly during the live OA and gives you a working solution as a safety net.
The problem
Apply every move in moves to the square 2048 grid. A move slides nonzero tiles toward its edge and merges each adjacent equal pair once. Do not spawn new tiles. Return the final grid. Function simulate2048(grid: int[][], moves: String) → int[][] Examples Example 1 grid = [[2,0,2,2],[0,0,0,0],[0,0,0,0],[0,0,0,0]] moves = "L" return = [[4,2,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] The first pair merges once and the third tile remains. Constraints 2 <= grid.length <= 20 and the grid is square. Every entry is zero or a positive power of two. moves contains only L, R, U, and D. For this exercise, assume the sum of all initial tile values is at most 2147483647. Moves conserve this sum because no tiles spawn.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to write one function that processes a single line toward the left, then reuse it for every direction. For L, process each row as is. For R, reverse the row, process it, reverse it back. For U and D, do the same on columns by extracting each column into a list. The line routine has three steps: filter out zeros, scan left to right merging an equal adjacent pair once and skipping the merged partner, then pad with zeros to the grid length. The common pitfall is chain merging, where a freshly merged 4 merges again with the next 4. Your scan must jump past both tiles after a merge. Another slip is mutating the grid while reading from it, so build a new grid per move. Complexity is O(moves * n^2), and n is at most 20, so performance isn't a concern. If the direction mapping gets tangled live, StealthCoder is the hedge.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill 2048 Move Simulator 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 StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Benchling's OA.
Benchling 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.
2048 Move Simulator FAQ
What's the trick in the Benchling 2048 Move Simulator?+
Reduce every direction to one left-slide on a single line. Compact the nonzero tiles, merge each adjacent equal pair once, then pad with zeros. For R, reverse the line first and after. For U and D, treat columns as lines the same way.
How do I avoid double merging in one move?+
After merging two tiles, skip past both before looking at the next one. In [2,2,4], the result is [4,4,0,0], never [8,0,0,0]. Use an index pointer that advances by two on a merge and by one otherwise, writing into a fresh output list.
How hard is this one really?+
Easy to medium. There's no clever algorithm, just careful simulation. Most failures come from off-by-one handling of merges or botched reversal for R and D. If you write the line helper first and test it on [2,0,2,2], the rest is mechanical.
Do I need to worry about overflow or large values?+
Not really. The statement says the sum of all initial tile values is at most 2147483647, and moves conserve that sum since no tiles spawn. A merged tile can never exceed the total, so a standard 32-bit int is safe for every value in the grid.
How do I prepare for this in 48 hours?+
Code the line-slide helper from scratch twice, then wire it to rows and columns with reversals. Test on the example, an all-equal row like [2,2,2,2], and an empty grid. Check that you return a new grid and don't mutate the input in unexpected ways.