← Glean Interview Insights

Glean·Frontend Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Interviewed for a frontend role at Glean and got a coding question straight out of a game mechanic. It was more involved than I expected for a frontend screen.

Questions Asked (1)

Q1

Implement the tile-sliding and merging logic from the 2048 game for all four swipe directions on a 4x4 grid, including the constraint that each tile can only merge once per move.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

I've played 2048 before so the rules weren't foreign to me, but actually coding it cleanly is a different thing.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem and edge cases, then propose a clean, reusable approach such as extracting each row/column into a temporary array, applying a single-direction merge algorithm, and writing back. Emphasize the 'merge once per move' constraint by tracking whether a tile has already merged in the current move, and discuss trade-offs between in-place manipulation and using helper arrays.

Pro tip: Mention that you would write unit tests for each direction and edge cases (e.g., [2,2,2,2] should become [4,4,0,0], not [8,0,0,0]) before coding, which shows production-level discipline and catches the merge-once bug early.

1. Clarify requirements and constraints

Confirm the grid size (4x4), tile values, and that each tile can merge only once per move. Ask about expected behavior for edge cases like multiple merges in a row and whether the grid should be mutated in place.

2. Design a reusable single-direction merge function

Write a function that takes an array of 4 values and returns the merged result for a left swipe, handling compression and merging with a 'merged' flag to prevent double merges. This function will be the core building block for all directions.

3. Map all four directions to the core function

For left/right, extract each row (reversing for right); for up/down, extract each column (reversing for down). Apply the core function and write the results back to the grid in the correct orientation.

4. Handle edge cases and validate

Test with cases like [2,2,2,2] -> [4,4,0,0], [2,2,4,4] -> [4,8,0,0], and no-op moves. Ensure the merge-once constraint is respected and that the grid updates correctly for all directions.

5. Discuss trade-offs and optimizations

Compare in-place manipulation vs. using temporary arrays for readability and performance. Mention time complexity O(n^2) for a 4x4 grid and space complexity O(n) for temporary storage, and consider whether to optimize for the small fixed size.

Key Points to Mention

  • The 'merge once per move' constraint: use a flag or check to ensure a tile that has already merged cannot merge again in the same move.
  • Extracting rows/columns and reusing a single-direction merge function to avoid code duplication and reduce bugs.
  • Handling direction transformations: reversing arrays for right/down swipes and transposing for vertical moves.
  • Edge cases: multiple merges in a row (e.g., [2,2,2,2]), no-op moves, and full/empty grids.
  • Time and space complexity: O(n^2) time for an n x n grid, with O(n) auxiliary space for temporary arrays.
  • Testing strategy: unit tests for each direction and edge cases to validate correctness.

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