Design a Tetris game with movement, rotation, hard drop and line clears

Quick Overview

Design the core logic of a Tetris game as a class that spawns tetrominoes, moves and rotates the active piece with collision checks, hard-drops it, and clears full rows. It tests object-oriented state modeling, grid handling and careful treatment of edge cases.

Design a Tetris game with movement, rotation, hard drop and line clears

Company: Redfin

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design the core logic for a Tetris game using object-oriented principles. The game is played on a rectangular board with `rows` rows and `cols` columns. Each cell is either empty or occupied by a settled block. A piece (tetromino) is one of the 7 standard shapes (`I`, `O`, `T`, `S`, `Z`, `J`, `L`). At any time there is at most one active piece on the board. Pieces spawn near the top-center of the board. The active piece can move left, right or down, or rotate clockwise, as long as it does not collide with the walls, the floor or settled cells. It can also be hard-dropped straight down to its lowest valid position. When a piece settles, every completely filled row is cleared, the rows above shift down, and the score increases by the number of rows cleared. The game tracks the board, the active piece, the score, and whether the game is over (a newly spawned piece overlaps settled blocks). Implement a class `TetrisGame(rows, cols)` with these methods: - `spawn(pieceType: str) -> bool`: spawn a new piece of the given type near the top-center. Return `False` and end the game if the spawn position collides with settled cells; otherwise return `True`. - `move(direction: str) -> bool`: move the active piece one cell in the given direction (`"LEFT"`, `"RIGHT"` or `"DOWN"`). Return `True` if the move succeeds. A failed `"DOWN"` move locks the piece, clears completed rows and updates the score. - `rotate() -> bool`: rotate the active piece 90 degrees clockwise. Return `True` if the rotated piece fits without collision; otherwise return `False` and leave the piece unchanged. - `drop() -> int`: hard-drop the active piece to its lowest valid position, lock it, clear completed rows, and return the number of rows cleared. - `getScore() -> int`: return the current score (total rows cleared so far). - `isGameOver() -> bool`: return `True` if a spawn has failed because of a collision. - `getBoard() -> List[List[int]]`: return the board as a 2D grid with `0` for an empty cell, `1` for a settled cell and `2` for a cell covered by the active piece. Example 1, on a 4 by 6 board. The second `LEFT` fails because the piece is already against the left wall. ```text Input: ["TetrisGame", "spawn", "move", "move", "drop", "getScore", "isGameOver"] [[4, 6], ["O"], ["LEFT"], ["LEFT"], [], [], []] Output: [null, true, true, false, 0, 0, false] game = TetrisGame(4, 6) game.spawn("O") # True: the O piece covers rows 0-1, columns 1-2 game.move("LEFT") # True: now columns 0-1 game.move("LEFT") # False: column -1 is outside the board, piece unchanged game.drop() # 0: the piece locks in rows 2-3, no row is full game.getScore() # 0 game.isGameOver() # False ``` Example 2, on a 5 by 5 board. ```text Input: ["TetrisGame", "spawn", "rotate", "getScore"] [[5, 5], ["T"], [], []] Output: [null, true, true, 0] game = TetrisGame(5, 5) game.spawn("T") # True game.rotate() # True: the rotated T still fits inside the board game.getScore() # 0: no rows cleared yet ``` Example 3, on a 4 by 4 board, where one drop clears two rows. ```text Input: ["TetrisGame", "spawn", "move", "move", "drop", "spawn", "drop", "getScore"] [[4, 4], ["O"], ["RIGHT"], ["RIGHT"], [], ["O"], [], []] Output: [null, true, true, true, 0, true, 2, 2] game = TetrisGame(4, 4) game.spawn("O") # True: columns 0-1 game.move("RIGHT") # True: columns 1-2 game.move("RIGHT") # True: columns 2-3 game.drop() # 0: locks in rows 2-3, columns 2-3 game.spawn("O") # True: rows 0-1 are still empty game.drop() # 2: rows 2 and 3 are now full and both are cleared game.getScore() # 2 ``` ```hint Share one collision check Spawning, moving, rotating and dropping can all be phrased the same way: work out the cells the piece would occupy, test them, then either commit the change or discard it. ``` ```hint Clearing several rows Watch what happens when two adjacent rows are full. Shifting rows in place while you scan them by index is an easy way to skip one. ``` ### Constraints & Assumptions - `4 <= rows <= 40` and `4 <= cols <= 20`. - `pieceType` is one of `"I"`, `"O"`, `"T"`, `"S"`, `"Z"`, `"J"`, `"L"`, and `direction` is one of `"LEFT"`, `"RIGHT"`, `"DOWN"`. - A game has at most ${10^4}$ operations. - Row 0 is the top of the board and `DOWN` increases the row index. There are no hidden rows above the board. - Each piece is drawn inside a small bounding box (4 by 4 for `I`, 2 by 2 for `O`, 3 by 3 for the other five). A new piece appears in its spawn orientation with the box's top-left corner at row 0 and column `cols // 2 - 2`. The examples follow this convention. - Rotation has no wall kicks: if the rotated cells collide, the rotation fails. ### Clarifying Questions to Ask - What should `spawn` do if a piece is still active? - What should `move`, `rotate` and `drop` return when there is no active piece, or after the game is over? - Is the score always the number of rows cleared, or do multi-row clears earn a bonus? - Does the caller drive gravity by issuing `DOWN` moves on a timer, or should the class own a clock? ### What a Strong Answer Covers - A clear split of state: the settled grid, the active piece (type, orientation, position), the score and the game-over flag, with the active piece kept separate from settled cells until it locks. - A piece representation that makes clockwise rotation and collision checks simple for all seven shapes. - One validity check against walls, floor and settled cells, reused by every operation, with a failed operation leaving the state unchanged. - Locking and row clearing that handle several full rows at once, update the score and keep the board size fixed. - Behavior on edge cases (no active piece, calls after game over, a failed `DOWN` that locks the piece) and the cost of each operation. ### Follow-up Questions - How would you add wall kicks so a rotation next to a wall or a settled block can still succeed? - How would you add a next-piece preview and a hold slot without changing the board logic? - How would you change scoring so clearing 4 rows at once is worth more than 4 single clears, and add levels that speed up gravity? - How would you test this class, and which cases would you pin down first?

Overview: Design the core logic of a Tetris game as a class that spawns tetrominoes, moves and rotates the active piece with collision checks, hard-drops it, and clears full rows. It tests object-oriented state modeling, grid handling and careful treatment of edge cases.

Redfin logo
Redfin
Nov 24, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design the core logic for a Tetris game using object-oriented principles. The game is played on a rectangular board with rows rows and cols columns. Each cell is either empty or occupied by a settled block.

A piece (tetromino) is one of the 7 standard shapes (I, O, T, S, Z, J, L). At any time there is at most one active piece on the board. Pieces spawn near the top-center of the board. The active piece can move left, right or down, or rotate clockwise, as long as it does not collide with the walls, the floor or settled cells. It can also be hard-dropped straight down to its lowest valid position.

When a piece settles, every completely filled row is cleared, the rows above shift down, and the score increases by the number of rows cleared. The game tracks the board, the active piece, the score, and whether the game is over (a newly spawned piece overlaps settled blocks).

Implement a class TetrisGame(rows, cols) with these methods:

  • spawn(pieceType: str) -> bool : spawn a new piece of the given type near the top-center. Return False and end the game if the spawn position collides with settled cells; otherwise return True .
  • move(direction: str) -> bool : move the active piece one cell in the given direction ( "LEFT" , "RIGHT" or "DOWN" ). Return True if the move succeeds. A failed "DOWN" move locks the piece, clears completed rows and updates the score.
  • rotate() -> bool : rotate the active piece 90 degrees clockwise. Return True if the rotated piece fits without collision; otherwise return False and leave the piece unchanged.
  • drop() -> int : hard-drop the active piece to its lowest valid position, lock it, clear completed rows, and return the number of rows cleared.
  • getScore() -> int : return the current score (total rows cleared so far).
  • isGameOver() -> bool : return True if a spawn has failed because of a collision.
  • getBoard() -> List[List[int]] : return the board as a 2D grid with 0 for an empty cell, 1 for a settled cell and 2 for a cell covered by the active piece.

Example 1, on a 4 by 6 board. The second LEFT fails because the piece is already against the left wall.

Input:
["TetrisGame", "spawn", "move",   "move",   "drop", "getScore", "isGameOver"]
[[4, 6],       ["O"],   ["LEFT"], ["LEFT"], [],     [],         []]

Output: [null, true, true, false, 0, 0, false]

game = TetrisGame(4, 6)
game.spawn("O")       # True: the O piece covers rows 0-1, columns 1-2
game.move("LEFT")     # True: now columns 0-1
game.move("LEFT")     # False: column -1 is outside the board, piece unchanged
game.drop()           # 0: the piece locks in rows 2-3, no row is full
game.getScore()       # 0
game.isGameOver()     # False

Example 2, on a 5 by 5 board.

Input:
["TetrisGame", "spawn", "rotate", "getScore"]
[[5, 5],       ["T"],   [],       []]

Output: [null, true, true, 0]

game = TetrisGame(5, 5)
game.spawn("T")       # True
game.rotate()         # True: the rotated T still fits inside the board
game.getScore()       # 0: no rows cleared yet

Example 3, on a 4 by 4 board, where one drop clears two rows.

Input:
["TetrisGame", "spawn", "move",    "move",    "drop", "spawn", "drop", "getScore"]
[[4, 4],       ["O"],   ["RIGHT"], ["RIGHT"], [],     ["O"],   [],     []]

Output: [null, true, true, true, 0, true, 2, 2]

game = TetrisGame(4, 4)
game.spawn("O")       # True: columns 0-1
game.move("RIGHT")    # True: columns 1-2
game.move("RIGHT")    # True: columns 2-3
game.drop()           # 0: locks in rows 2-3, columns 2-3
game.spawn("O")       # True: rows 0-1 are still empty
game.drop()           # 2: rows 2 and 3 are now full and both are cleared
game.getScore()       # 2

Constraints & Assumptions

  • 4 <= rows <= 40 and 4 <= cols <= 20 .
  • pieceType is one of "I" , "O" , "T" , "S" , "Z" , "J" , "L" , and direction is one of "LEFT" , "RIGHT" , "DOWN" .
  • A game has at most 104{10^4} operations.
  • Row 0 is the top of the board and DOWN increases the row index. There are no hidden rows above the board.
  • Each piece is drawn inside a small bounding box (4 by 4 for I , 2 by 2 for O , 3 by 3 for the other five). A new piece appears in its spawn orientation with the box's top-left corner at row 0 and column cols // 2 - 2 . The examples follow this convention.
  • Rotation has no wall kicks: if the rotated cells collide, the rotation fails.

Clarifying Questions to Ask Guidance

  • What should spawn do if a piece is still active?
  • What should move , rotate and drop return when there is no active piece, or after the game is over?
  • Is the score always the number of rows cleared, or do multi-row clears earn a bonus?
  • Does the caller drive gravity by issuing DOWN moves on a timer, or should the class own a clock?

What a Strong Answer Covers Guidance

  • A clear split of state: the settled grid, the active piece (type, orientation, position), the score and the game-over flag, with the active piece kept separate from settled cells until it locks.
  • A piece representation that makes clockwise rotation and collision checks simple for all seven shapes.
  • One validity check against walls, floor and settled cells, reused by every operation, with a failed operation leaving the state unchanged.
  • Locking and row clearing that handle several full rows at once, update the score and keep the board size fixed.
  • Behavior on edge cases (no active piece, calls after game over, a failed DOWN that locks the piece) and the cost of each operation.

Follow-up Questions Guidance

  • How would you add wall kicks so a rotation next to a wall or a settled block can still succeed?
  • How would you add a next-piece preview and a hold slot without changing the board logic?
  • How would you change scoring so clearing 4 rows at once is worth more than 4 single clears, and add levels that speed up gravity?
  • How would you test this class, and which cases would you pin down first?