← Glean Interview Insights

Glean·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Apr 2026

Summary

Interviewed for a software engineer role at Glean and got a 2048 implementation problem, which I thought was kind of a fun twist on the usual grid stuff. The edge cases around merging are where it gets tricky and where I spent most of my time fumbling.

Questions Asked (1)

Q1

Implement the tilt/swipe operation for a 2048 board. Given a 4x4 grid and a direction, shift and merge tiles correctly, respecting the rule that each tile can only merge once per move.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

I knew 2048 as a game but had never thought about coding the move logic.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the rules and edge cases, then propose a clean algorithm that processes each row/column in the direction of movement, compacts non-zero tiles, merges adjacent equal tiles (tracking merged flags), and compacts again. Implement a helper that operates on a single line and apply it to all lines (rows or columns) with appropriate direction handling.

Pro tip: Mention that you can avoid duplicating logic for all four directions by extracting a line-processing function and using transformations (reverse, transpose) to map any direction to a single canonical left-shift operation. This demonstrates code reuse and reduces bugs.

1. Clarify requirements and edge cases

Confirm the rules: each tile merges at most once per move, merges happen in the direction of movement, and zeros represent empty cells. Discuss edge cases like full board, no possible moves, and multiple merges in a line.

2. Design a line-based algorithm

Create a function that processes a single line (array of 4 values) for a left shift: remove zeros, merge adjacent equal values (skipping the next element after a merge), then pad with zeros. This ensures each tile merges only once.

3. Generalize to all directions

For each direction, extract lines (rows for left/right, columns for up/down), apply the line function (reversing the line for right/down), and write back. Alternatively, use matrix transformations (transpose, reverse) to reuse the left-shift logic.

4. Implement and test

Write clean code with helper functions, then test with representative cases: simple shifts, merges, multiple merges in one line, and no-op moves. Verify that the merge-once rule holds.

5. Analyze complexity and trade-offs

State that the solution is O(n^2) for an n x n board (here n=4), which is optimal since every cell must be examined. Discuss trade-offs between in-place modification and creating a new board, and between code duplication and abstraction.

Key Points to Mention

  • The merge-once rule: after merging two tiles, skip the next tile to prevent double merging.
  • Line-based processing: extract rows/columns, process each independently, then write back.
  • Direction handling: use reversal and transposition to map all directions to a left-shift operation.
  • Time and space complexity: O(n^2) time, O(1) extra space if in-place, or O(n^2) if creating a new board.
  • Edge cases: empty board, full board with no merges, multiple merges in a single line.
  • Code organization: separate concerns into helper functions for clarity and testability.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.