Check Whether the Latest Move Creates Four in a Row

Read the full interview experience this question came from →

Quick Overview

Check four-in-a-row through the latest move using four axes, bounded bidirectional counts, edge handling, and constant work for a fixed target length.

Check Whether the Latest Move Creates Four in a Row

Company: Snowflake

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Given a board, the current move's row and column, and its player, determine whether that move forms at least four consecutive pieces of that player horizontally, vertically, or along either diagonal. Implement `did_player_connect_four(board: int[][], row: int, col: int, player: int) -> bool`. ### Constraints & Assumptions - The board is rectangular with at least one row and column and at most 1000000 cells. Each cell is 0 (empty), 1, or 2. - Player is 1 or 2, the coordinates are valid, and the move has already been placed: `board[row][col] == player`. - A winning line must include the current move. An unrelated existing four-in-a-row elsewhere does not make this call true. - Runs longer than four also count. No gravity, turn legality, or full-game validation is required. - Four is a fixed target. Aim for O(1) time and O(1) extra space by checking only a bounded neighborhood of the current move. ### Examples ```text board = [[2,2,2,2]], row = 0, col = 3, player = 2 result = true ``` ```text board = [[1,1,1,1],[0,0,0,2]], row = 1, col = 3, player = 2 result = false ``` Explain how opposite directions combine around the current piece, how boundaries stop counting, and why no more than three cells per side are necessary for a fixed four-piece target. Include horizontal, vertical, both diagonals, split runs around the move, and edge/corner tests. Compare the cost if the target length were an input instead of fixed.

Overview: Check four-in-a-row through the latest move using four axes, bounded bidirectional counts, edge handling, and constant work for a fixed target length.

Read the full Snowflake Software Engineer interview experience this question came from

|Home/Coding & Algorithms/Snowflake
Snowflake logo
Snowflake
Sep 20, 2026
mediumSoftware EngineerTechnical ScreenCoding & Algorithms
0
0

Given a board, the current move's row and column, and its player, determine whether that move forms at least four consecutive pieces of that player horizontally, vertically, or along either diagonal.

Implement did_player_connect_four(board: int[][], row: int, col: int, player: int) -> bool.

Constraints & Assumptions

  • The board is rectangular with at least one row and column and at most 1000000 cells. Each cell is 0 (empty), 1, or 2.
  • Player is 1 or 2, the coordinates are valid, and the move has already been placed: board[row][col] == player .
  • A winning line must include the current move. An unrelated existing four-in-a-row elsewhere does not make this call true.
  • Runs longer than four also count. No gravity, turn legality, or full-game validation is required.
  • Four is a fixed target. Aim for O(1) time and O(1) extra space by checking only a bounded neighborhood of the current move.

Examples

board = [[2,2,2,2]], row = 0, col = 3, player = 2
result = true
board = [[1,1,1,1],[0,0,0,2]], row = 1, col = 3, player = 2
result = false

Explain how opposite directions combine around the current piece, how boundaries stop counting, and why no more than three cells per side are necessary for a fixed four-piece target. Include horizontal, vertical, both diagonals, split runs around the move, and edge/corner tests. Compare the cost if the target length were an input instead of fixed.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...