Merge a 2048 Row
Updated
Problem statement
Return the result of one left move on a single row of 2048 tiles.
- Move every nonzero tile left, keeping its relative order.
- Scan those tiles from left to right. If the next two tiles have the same value, replace them with one tile of twice that value. Each tile can take part in at most one merge during this move.
- Fill the remaining positions on the right with zeros. The returned row has the same length as the input.
For this exercise, assume the move adds no random tile. An empty row returns an empty row.
Function
mergeRow(row: int[]) → int[]Examples
Example 1
row = [2,0,2,0]return = [4,0,0,0]Removing zeros leaves [2,2]. These two tiles merge into 4, followed by three zeros.
Example 2
row = [2,2,4,4]return = [4,8,0,0]The first two tiles merge into 4. The next two merge into 8. Neither new tile merges again during this move.
Example 3
row = [2,4,2,4]return = [2,4,2,4]No adjacent nonzero tiles have the same value, so the row stays unchanged.
Constraints
0 <= row.length <= 10^4.0 <= row[i] <= 2^20.- Each nonzero value is a power of two.
More Palantir interview questions
- Query Words Within K PositionsPHONE SCREEN · Seen Nov 2026
- Balanced Session ManagerPHONE SCREEN · Seen Oct 2026
- Employees With Shared InterestsPHONE SCREEN · Seen Jul 2025
- Encrypt Letters by First-Occurrence SubstitutionPHONE SCREEN · Seen Aug 2022
- Encrypt Text by Columnar TranspositionPHONE SCREEN · Seen Aug 2022
- Basic Item DiscountsOA · Seen Nov 2026
- Optimize Discount Selection Under a Point BudgetOA · Seen Nov 2026
- Stacked Item and Category DiscountsOA · Seen Nov 2026
- Map and Accumulate Learning QueriesONSITE INTERVIEW · Seen Oct 2026
- Relational Learning QueriesONSITE INTERVIEW · Seen Apr 2025
Similar questions at other companies
- Candy Crush Grid Matching and GravityRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- Add Signed Character ArraysByteDance · PHONE SCREENByteDance interview questions
- Nested List Iterator with RemoveAirbnb · PHONE SCREENAirbnb interview questions
- DMA Stream DistributionEtched · PHONE SCREEN
- Catch Fish with Reusable BaitsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Collect Branches for a Bird NestVisa · OAVisa interview questions
- Alternate Positive and Negative ValuesTesla · ONSITE INTERVIEWTesla interview questions
- Score Difference When Even Picks Reverse the ListMicrosoft · OAMicrosoft interview questions
- Sum Two Piecewise-Constant Time SeriesGoogle · ONSITE INTERVIEWGoogle interview questions
- Match OutcomesAdobe · OAAdobe interview questions