FastPrepDetect a Winner on a 3x3 Tic-Tac-Toe Board

Detect a Winner on a 3x3 Tic-Tac-Toe Board

Hebbia logoHebbia● EasyFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Given a completed or partially completed 3 × 3 Tic-Tac-Toe board, return X if X occupies a complete row, column, or diagonal, return O if O does, and otherwise return None.

Each row is a string containing only X, O, and .. The board is reachable from legal alternating play, so it cannot contain simultaneous winners.

Function

ticTacToeWinner(board: String[]) → String

Examples

Example 1

board = ["XXX","O.O","..."]return = "X"

X fills the first row.

Example 2

board = ["XO.","XO.",".O."]return = "O"

O fills the middle column.

Example 3

board = ["XO.","OX.","..."]return = "None"

No row, column, or diagonal is complete.

Constraints

  • board.length == 3
  • board[i].length == 3
  • Every character is X, O, or ..
  • The board has no simultaneous winners.

More Hebbia problems

See Hebbia hiring insights
public String ticTacToeWinner(String[] board) {
  // Write your code here.
}
board["XXX","O.O","..."]
expected"X"
Checking account…