Tic-Tac-Toe Winner on a Huge Board
Updated
Problem statement
Two players, X and O, play tic-tac-toe on a square board with 2^31 rows and 2^31 columns, numbered from 0 to 2^31 - 1. The board starts empty.
You are given the moves in the order they were played: moves[i] = [row, col] is the cell marked on move i. X moves first and the players take turns, so X makes the moves at even indexes and O makes the moves at odd indexes. A player wins as soon as k or more of their marks stand in a row with no gap: along a row, along a column, or along either diagonal.
Return "X" or "O" for the player who won. If nobody has won after the last move, return "Pending". A game this short can never fill the board, so there is no draw.
Function
ticTacToeWinner(moves: int[][], k: int) → StringExamples
Example 1
moves = [[0,0],[5,5],[1,1],[5,6],[2,2]]k = 3return = "X"X makes the moves at indexes 0, 2 and 4: [0,0], [1,1] and [2,2]. After index 4 these three cells form a diagonal line of 3, so X wins.
Example 2
moves = [[2147483647,2147483647],[0,2147483645],[7,7],[0,2147483646],[100,3],[0,2147483647]]k = 3return = "O"O marks [0,2147483645], [0,2147483646] and [0,2147483647]: three cells side by side in row 0, ending at the last column. The last move, at index 5, completes the line, so O wins. One step to the right of column 2147483647 is off the board.
Example 3
moves = [[10,10],[0,0],[10,11],[0,1],[10,12],[1,0]]k = 4return = "Pending"X has 3 marks in a row, [10,10], [10,11] and [10,12], but k = 4. O has no line of 4 either, so nobody has won yet.
Example 4
moves = [[1,3],[0,0],[2,3],[9,9],[4,3],[0,9],[3,3]]k = 4return = "X"Before the last move, X holds [1,3], [2,3] and [4,3] in column 3, with a gap at row 3. The last move fills [3,3]: counting up from it gives 2 more marks (rows 2 and 1) and counting down gives 1 more (row 4), so the column holds 4 in a row and X wins.
Constraints
1 <= moves.length <= 5000moves[i].length == 20 <= moves[i][0], moves[i][1] <= 2147483647(that is,2^31 - 1)3 <= k <= 5000- All cells in
movesare different. - The game stops as soon as a player wins, so no move comes after a winning move.
More Zillow Onsite Interview questions
- Valid ParenthesesPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2025
- Build a Hash TablePHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2023
- Spiral MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2022
- First Subarray with the Largest SumOA · ONSITE INTERVIEW · Seen Aug 2021
- Count Duplicate CharactersONSITE INTERVIEW · Seen Oct 2020
- Decision Tree Classifier from ScratchONSITE INTERVIEW · Seen Nov 2019
- Find Median from Data StreamPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2018
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2016
- Most Frequent Three-Page SequencesPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2016
- Maximum Path Sum in a GridONSITE INTERVIEW · Seen Apr 2016
Similar questions at other companies
- Rectangle Point Coverage CountsOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Configurable Tic-Tac-Toe GameEliseAI · PHONE SCREEN
- Game of Life on a Finite and an Infinite BoardCitadel · PHONE SCREENCitadel interview questions
- BitFont Part 2 - Render a WordStripe · ONSITE INTERVIEWStripe interview questions
- Layered Rectangle CanvasRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Cache Shared Image Pipeline PrefixesAnthropic · ONSITE INTERVIEWAnthropic interview questions
- First Completed Mingo LineEpic · OAEpic interview questions
- Hidden ArtifactsIMC Trading · OAIMC Trading interview questions
- Sustained Overspeed AlertsFleetx · ONSITE INTERVIEW
- LRU CacheAthenahealth · PHONE SCREEN