FastPrepFastPrep
Problem Brief

Simplified Tetris Engine

FULLTIMEOA

You are building a simplified Tetris engine for a grid that is 10 columns wide. Pieces enter from the top, fall straight down, and come to rest as soon as any part of the piece touches the bottom of the grid or an already-settled block.

Each piece is rigid, consists of four unit squares, and never rotates. The possible piece types are Q, Z, S, T, I, L, and J. A game state starts with an empty grid.

The input to your function is a comma-separated sequence of piece placements. Each placement is encoded as a piece letter followed by a single-digit integer. The integer is the leftmost column occupied by the piece, using zero-based indexing. After each piece settles, if any row becomes completely full, that row is cleared and every row above it drops down together without changing the internal block pattern within each row.

Return the final height of the remaining settled blocks after all placements in the sequence have been processed.

Function Description

Complete the function getFinalHeight in the editor below.

getFinalHeight has the following parameter:

  • String sequence: a comma-separated sequence of piece placements such as "I0,I4,Q8"

Returns

int: the final stack height after processing all placements and clearing all complete rows

1Example 1

Input
sequence = "I0,I4,Q8"
Output
1
Explanation
The three pieces fill the entire bottom row, so that row is cleared. The remaining settled blocks have height 1.

2Example 2

Input
sequence = "T1,Z3,I4"
Output
4
Explanation
No row becomes completely full, so nothing is cleared. The final settled structure has height 4.

3Example 3

Input
sequence = "Q0,I2,I6,I0,I6,I6,Q2,Q4"
Output
3
Explanation
The first row clears after the early placements, and a later row clears again after additional pieces settle. The final remaining structure has height 3.

Constraints

Limits and guarantees your solution can rely on.

  • The grid width is always 10.
  • Each placement is one of Q, Z, S, T, I, L, or J, followed by a single-digit zero-based column index.
  • Pieces never rotate and always use the fixed orientations shown in the source prompt.
  • The input sequence is valid, and every placement fits within the board.
  • You may assume no test case produces a final height greater than 100.
public int getFinalHeight(String sequence) {
  // write your code here
}
Input

sequence

"I0,I4,Q8"

Output

1

Sign in to submit your solution.