PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/Coding & Algorithms/Glean

Implement 2048 Game Logic

Last updated: May 2, 2026

Quick Overview

This question evaluates array and matrix manipulation, stateful simulation of game mechanics, and handling edge cases such as merge constraints, single-merge-per-move rules, and empty-cell management.

  • medium
  • Glean
  • Coding & Algorithms
  • Software Engineer

Implement 2048 Game Logic

Company: Glean

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Implement the core logic for the 2048 game. You are given an `n x n` integer board where `0` represents an empty cell and every nonzero value represents a tile value. Implement a method such as `move(direction)` that updates the board after one move in one of four directions: `up`, `down`, `left`, or `right`. Rules: - All tiles slide as far as possible in the chosen direction. - Adjacent tiles with the same value merge into one tile with double the value. - A tile may participate in at most one merge per move. - Empty cells are filled with `0` after sliding and merging. - You do not need to implement the UI. If random new-tile generation is needed, keep it separate from the deterministic movement logic. Examples for a left move on a single row: - `[2, 0, 2, 0] -> [4, 0, 0, 0]` - `[2, 2, 2, 0] -> [4, 2, 0, 0]` - `[2, 2, 2, 2] -> [4, 4, 0, 0]` Follow-up: implement `isGameOver()`, which returns `true` if no legal move remains. A game is over when the board has no empty cells and there are no horizontally or vertically adjacent equal tiles.

Quick Answer: This question evaluates array and matrix manipulation, stateful simulation of game mechanics, and handling edge cases such as merge constraints, single-merge-per-move rules, and empty-cell management.

Related Interview Questions

  • Search Words in a Character Grid - Glean (hard)
  • Find the Kth Largest in Two Sorted Arrays - Glean (medium)
  • Implement 2048 tilt move - Glean (hard)
  • Simulate document assignment to indexers - Glean (hard)
  • Determine Reachability in Train Schedule - Glean (medium)
Glean logo
Glean
Mar 1, 2026, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
5
0

Implement the core logic for the 2048 game.

You are given an n x n integer board where 0 represents an empty cell and every nonzero value represents a tile value. Implement a method such as move(direction) that updates the board after one move in one of four directions: up, down, left, or right.

Rules:

  • All tiles slide as far as possible in the chosen direction.
  • Adjacent tiles with the same value merge into one tile with double the value.
  • A tile may participate in at most one merge per move.
  • Empty cells are filled with 0 after sliding and merging.
  • You do not need to implement the UI. If random new-tile generation is needed, keep it separate from the deterministic movement logic.

Examples for a left move on a single row:

  • [2, 0, 2, 0] -> [4, 0, 0, 0]
  • [2, 2, 2, 0] -> [4, 2, 0, 0]
  • [2, 2, 2, 2] -> [4, 4, 0, 0]

Follow-up: implement isGameOver(), which returns true if no legal move remains. A game is over when the board has no empty cells and there are no horizontally or vertically adjacent equal tiles.

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Glean•More Software Engineer•Glean Software Engineer•Glean Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,500+ 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
  • Compare Platforms
  • Discord Community

Support

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

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.