Simulate inverted-gravity Connect Four and return the final board

Quick Overview

Simulate a Connect Four variant on a 6 x 7 board where discs enter from the bottom and float up, stacking from the top row, then return the final grid. It tests careful row and column indexing, turn tracking and keeping per-column state during a short simulation.

Simulate inverted-gravity Connect Four and return the final board

Company: Jane Street

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are simulating a variant of Connect Four on a 6 x 7 grid (6 rows, 7 columns). Two players, `R` (Red) and `Y` (Yellow), take turns adding discs to the board. Red always moves first. In standard Connect Four, discs fall down under gravity. In this variant gravity is inverted: discs are pushed in from the bottom of the board and float upward, stacking from the top. When a player adds a disc to a column, it occupies the highest available row in that column, which is the lowest unoccupied row index when row 0 is the top. Given an array `moves` where `moves[i]` is the column (0-indexed from the left) chosen on turn `i`, return the final state of the board as a 6 x 7 grid of strings. Each cell is `"R"`, `"Y"` or `""` (empty). Row 0 is the top of the board and row 5 is the bottom. ### Function Signature ```python def connect_four_up(moves: list[int]) -> list[list[str]]: ``` ### Rules - Turn `i` is played by `R` when `i` is even and by `Y` when `i` is odd. The color depends only on the turn index. - Return exactly 6 lists of 7 strings each, ordered from row 0 (top) to row 5 (bottom), with columns ordered from left to right. - An empty cell is the empty string `""`. ### Constraints - `1 <= len(moves) <= 42` - `0 <= moves[i] <= 6` - Moves alternate, starting with `R`, then `Y`. - Every move is valid: the chosen column has at least one empty cell when the move is played. ### Examples ```text Input: moves = [3, 3, 3, 3] Output: [ ["", "", "", "R", "", "", ""], ["", "", "", "Y", "", "", ""], ["", "", "", "R", "", "", ""], ["", "", "", "Y", "", "", ""], ["", "", "", "", "", "", ""], ["", "", "", "", "", "", ""] ] ``` Four discs are dropped into column 3. Because gravity is inverted, they stack from the top down, so rows 0 to 3 of that column read R, Y, R, Y. ```text Input: moves = [0, 1, 2, 3, 4, 5, 6] Output: [ ["R", "Y", "R", "Y", "R", "Y", "R"], ["", "", "", "", "", "", ""], ["", "", "", "", "", "", ""], ["", "", "", "", "", "", ""], ["", "", "", "", "", "", ""], ["", "", "", "", "", "", ""] ] ``` Seven discs are placed, one per column, alternating R and Y starting with R. Each one lands in the top row of its column. ```text Input: moves = [2, 2, 5, 2, 5] Output: [ ["", "", "R", "", "", "R", ""], ["", "", "Y", "", "", "R", ""], ["", "", "Y", "", "", "", ""], ["", "", "", "", "", "", ""], ["", "", "", "", "", "", ""], ["", "", "", "", "", "", ""] ] ``` Column 2 receives turns 0, 1 and 3 (R, Y, Y), and column 5 receives turns 2 and 4 (R, R). Since the color comes from the turn index, column 5 ends up with two R discs stacked together.

Overview: Simulate a Connect Four variant on a 6 x 7 board where discs enter from the bottom and float up, stacking from the top row, then return the final grid. It tests careful row and column indexing, turn tracking and keeping per-column state during a short simulation.

Jane Street logo
Jane Street
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are simulating a variant of Connect Four on a 6 x 7 grid (6 rows, 7 columns). Two players, R (Red) and Y (Yellow), take turns adding discs to the board. Red always moves first.

In standard Connect Four, discs fall down under gravity. In this variant gravity is inverted: discs are pushed in from the bottom of the board and float upward, stacking from the top. When a player adds a disc to a column, it occupies the highest available row in that column, which is the lowest unoccupied row index when row 0 is the top.

Given an array moves where moves[i] is the column (0-indexed from the left) chosen on turn i, return the final state of the board as a 6 x 7 grid of strings. Each cell is "R", "Y" or "" (empty). Row 0 is the top of the board and row 5 is the bottom.

Function Signature

def connect_four_up(moves: list[int]) -> list[list[str]]:

Rules

  • Turn i is played by R when i is even and by Y when i is odd. The color depends only on the turn index.
  • Return exactly 6 lists of 7 strings each, ordered from row 0 (top) to row 5 (bottom), with columns ordered from left to right.
  • An empty cell is the empty string "" .

Constraints

  • 1 <= len(moves) <= 42
  • 0 <= moves[i] <= 6
  • Moves alternate, starting with R , then Y .
  • Every move is valid: the chosen column has at least one empty cell when the move is played.

Examples

Input: moves = [3, 3, 3, 3]
Output:
[
  ["", "", "", "R", "", "", ""],
  ["", "", "", "Y", "", "", ""],
  ["", "", "", "R", "", "", ""],
  ["", "", "", "Y", "", "", ""],
  ["", "", "", "", "", "", ""],
  ["", "", "", "", "", "", ""]
]

Four discs are dropped into column 3. Because gravity is inverted, they stack from the top down, so rows 0 to 3 of that column read R, Y, R, Y.

Input: moves = [0, 1, 2, 3, 4, 5, 6]
Output:
[
  ["R", "Y", "R", "Y", "R", "Y", "R"],
  ["", "", "", "", "", "", ""],
  ["", "", "", "", "", "", ""],
  ["", "", "", "", "", "", ""],
  ["", "", "", "", "", "", ""],
  ["", "", "", "", "", "", ""]
]

Seven discs are placed, one per column, alternating R and Y starting with R. Each one lands in the top row of its column.

Input: moves = [2, 2, 5, 2, 5]
Output:
[
  ["", "", "R", "", "", "R", ""],
  ["", "", "Y", "", "", "R", ""],
  ["", "", "Y", "", "", "", ""],
  ["", "", "", "", "", "", ""],
  ["", "", "", "", "", "", ""],
  ["", "", "", "", "", "", ""]
]

Column 2 receives turns 0, 1 and 3 (R, Y, Y), and column 5 receives turns 2 and 4 (R, R). Since the color comes from the turn index, column 5 ends up with two R discs stacked together.