Design Tetris
🧱 What is Tetris? It is the falling-blocks game. Pieces made of four squares drop one at a time into a tall, narrow well. The player slides and rotates each piece as it falls. When a piece lands it locks in place, and any row that is completely filled disappears. The game ends when a new piece has no room to appear.
Tetris is a good medium problem because it looks like a pure graphics game but is really about state and rules: where the falling piece is, what counts as a collision, what happens when it lands, and who decides when time passes. A clean design keeps the falling piece separate from the locked cells and keeps the clock out of the game rules.
Requests for "Tetris pieces", "rotation", "line clears" and "the game loop" are all parts of this one design. Pieces and rotation are the first choice in the Implementation section, line clears get their own section, and the game loop is where we decide how time reaches the game.
Requirements
"Design the game Tetris."
That is often the whole prompt. The questions below turn it into rules we can code and test.
Clarifying Questions
You: How big is the board?
Interviewer: Make it configurable. The classic board is 10 columns by 20 rows.
You: Which pieces are there, and in what order do they come?
Interviewer: The seven standard ones: I, O, T, S, Z, J and L. In the real game the order is random, but I want to be able to replay a game exactly, so let the order come from something we control.
You: How does rotation work? If a piece is next to a wall and rotating would push it into the wall, what happens?
Interviewer: Rotate clockwise, in place. If the rotated piece does not fit, the rotation simply fails. Fancier rules can come later.
You: When does a piece lock?
Interviewer: When gravity tries to move it down and it cannot. The player can also hard-drop: the piece falls all the way and locks at once.
You: How does scoring work?
Interviewer: Points for rows cleared by one piece: 100 for one row, 300 for two, 500 for three, 800 for four.
You: How does time work? Should the game read a clock?
Interviewer: You tell me. Pieces fall one row every so often, and that speed may change.
You: When is the game over?
Interviewer: When a new piece cannot appear because cells are in the way.
The time question is the one interviewers use to separate candidates. We will answer it by giving the game a tick() method, "one step of gravity", and keeping the real clock in a small loop outside the game. That makes the rules easy to test and leaves the speed in one place.
💡 Interview tip: Whenever a game has time in it, ask how time reaches the code. "The game has a
tick()and something outside calls it" is almost always the right answer, and it is how the practice tests drive this game.
Final Requirements
- The board has a configurable width and height. It starts empty.
- There are seven piece types. Pieces arrive in an order given by a piece source; the next piece appears at the top as soon as the previous one locks.
- The player can move the falling piece left or right one column, or rotate it clockwise. A move or rotation that would leave the board or overlap a locked cell is refused.
- Each tick of gravity moves the piece down one row. If it cannot move down, it locks.
- A hard drop moves the piece down as far as it can go and locks it immediately.
- When a piece locks, every full row is removed and the rows above it fall down.
- Clearing 1, 2, 3 or 4 rows with one piece scores 100, 300, 500 or 800 points.
- If a new piece overlaps a locked cell when it appears, the game is over, and all further input is ignored.
Out of scope
- Wall kicks, a hold piece, a next-piece preview and levels that speed up (Extensibility).
- Soft-drop points, special moves like T-spins, and combos.
- Graphics, sound and input devices.
- Two-player battles.