PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates a candidate's ability to implement array and matrix manipulation, deterministic state-transition logic, and rule-based merging behavior in a grid-based simulation.

  • hard
  • Glean
  • Coding & Algorithms
  • Software Engineer

Implement 2048 tilt move

Company: Glean

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

## Problem: Implement a 2048 “tilt” (move) operation You are given a **4×4** board for the game 2048. Each cell contains either **0** (empty) or a **power of two** (2, 4, 8, ...). When the player swipes in one of four directions (`up`, `down`, `left`, `right`), the board updates as follows: 1. **Shift:** All non-zero tiles move as far as possible in the swipe direction. 2. **Merge:** If two tiles with the **same value** collide during the move, they **merge into one tile** with value equal to the **sum** (i.e., doubled). 3. **Single-merge rule:** A tile can be merged **at most once per move**. 4. After merges, tiles continue shifting to fill gaps in the swipe direction. ### Task Implement a function that applies exactly **one** swipe (tilt) to the board. ### Function signature (language-agnostic) - Input: - `board`: 4×4 integer matrix - `dir`: one of `{up, down, left, right}` - Output: - The updated 4×4 board after the move ### Examples - Swipe left: - `[2, 0, 2, 4]` → `[4, 4, 0, 0]` - Swipe left (single-merge rule): - `[2, 2, 2, 0]` → `[4, 2, 0, 0]` (not `[8, 0, 0, 0]`) - Swipe left: - `[4, 4, 4, 4]` → `[8, 8, 0, 0]` ### Constraints / Notes - Board size is always 4×4. - Only implement the tilt/move logic (no random tile spawning, no scoring required unless you choose to return it as an extra). - Be careful with edge cases around multiple merges in a line and ensuring each tile merges at most once per move.

Quick Answer: This question evaluates a candidate's ability to implement array and matrix manipulation, deterministic state-transition logic, and rule-based merging behavior in a grid-based simulation.

You are given a 4x4 board for the game 2048. Each cell contains either 0 (empty) or a power of two (2, 4, 8, ...). When the player swipes in one of four directions ('up', 'down', 'left', 'right'), the board updates as follows: 1. Shift: All non-zero tiles move as far as possible in the swipe direction. 2. Merge: If two tiles with the same value collide during the move, they merge into one tile whose value is their sum (i.e. doubled). 3. Single-merge rule: A tile can be merged at most once per move. 4. After merges, tiles continue shifting to fill gaps in the swipe direction. Implement a function `tilt(board, dir)` that applies exactly one swipe to the board and returns the updated 4x4 board. `dir` is one of the strings 'up', 'down', 'left', 'right'. Examples (single row, swipe left): - [2, 0, 2, 4] -> [4, 4, 0, 0] - [2, 2, 2, 0] -> [4, 2, 0, 0] (single-merge rule, NOT [8, 0, 0, 0]) - [4, 4, 4, 4] -> [8, 8, 0, 0] Merges resolve from the swipe-edge inward: when scanning a line in the direction of travel, the first eligible same-value pair merges, and a tile that has already merged cannot merge again in the same move.

Constraints

  • The board is always 4x4.
  • Each cell is 0 or a power of two (2, 4, 8, ...).
  • dir is exactly one of 'up', 'down', 'left', 'right'.
  • A tile may merge at most once per move.
  • Only the tilt/move logic is required — no random tile spawning and no scoring.

Examples

Input: ([[2,0,2,4],[2,2,2,0],[4,4,4,4],[0,0,0,0]], 'left')

Expected Output: [[4, 4, 0, 0], [4, 2, 0, 0], [8, 8, 0, 0], [0, 0, 0, 0]]

Explanation: Swipe left. Row [2,0,2,4]: the two 2s merge to 4, giving [4,4,0,0]. Row [2,2,2,0]: leftmost pair merges to 4, the third 2 cannot merge again -> [4,2,0,0]. Row [4,4,4,4]: two independent merges -> [8,8,0,0]. Empty row stays empty.

Input: ([[2,0,2,4],[2,2,2,0],[4,4,4,4],[0,0,0,0]], 'right')

Expected Output: [[0, 0, 4, 4], [0, 0, 2, 4], [0, 0, 8, 8], [0, 0, 0, 0]]

Explanation: Swipe right is the mirror of left. Row [2,2,2,0] now merges its rightmost pair, leaving the stray 2 to the left -> [0,0,2,4]. Tiles pack toward the right edge.

Input: ([[2,2,4,0],[2,0,4,0],[0,2,0,8],[2,2,0,8]], 'up')

Expected Output: [[4, 4, 8, 16], [2, 2, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

Explanation: Swipe up merges within each column toward row 0. Column 0 [2,2,0,2] -> 2+2 merge then trailing 2 -> [4,2,0,0]. Column 2 [4,4,0,0] -> [8,0,0,0]. Column 3 [0,0,8,8] -> [16,0,0,0].

Input: ([[2,2,4,0],[2,0,4,0],[0,2,0,8],[2,2,0,8]], 'down')

Expected Output: [[0, 0, 0, 0], [0, 0, 0, 0], [2, 2, 0, 0], [4, 4, 8, 16]]

Explanation: Swipe down packs and merges columns toward row 3. Column 0 [2,2,0,2] -> bottom pair merges -> [0,0,2,4]. Column 3 [0,0,8,8] -> [0,0,0,16].

Input: ([[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]], 'left')

Expected Output: [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

Explanation: Edge case: an all-empty board is unchanged in every direction.

Input: ([[2,2,2,2],[8,8,8,8],[2,4,8,16],[0,2,0,2]], 'right')

Expected Output: [[0, 0, 4, 4], [0, 0, 16, 16], [2, 4, 8, 16], [0, 0, 0, 4]]

Explanation: Swipe right. [2,2,2,2] and [8,8,8,8] each form two independent merges packed right. [2,4,8,16] has no equal neighbors so it only packs (already full). [0,2,0,2] -> the two 2s merge -> [0,0,0,4].

Hints

  1. Reduce the 2D problem to a 1D one: solve 'slide and merge a single line toward index 0', then apply it to the correct row/column orientation per direction.
  2. For a single line: first drop the zeros to pack non-zero tiles together, then scan left-to-right merging equal adjacent pairs, advancing past a merged pair so the same tile is never merged twice.
  3. Handle 'right' by reversing each row, applying the left-slide, then reversing back. Handle 'up'/'down' by transposing to columns (and reversing for 'down').
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Implement a Byte Pair Encoding (BPE) Tokenizer - Glean (medium)
  • Return Top Department Suggestions - Glean (medium)
  • Implement Rate-Limited Wikipedia Crawler - Glean (medium)
  • Find Earliest Train Route - Glean (medium)
  • Search Words in a Character Grid - Glean (hard)