Skip to content
Home ยป Top Amazon SDE 2 Interview Problem: Tic Tac Toe Game Logic

Top Amazon SDE 2 Interview Problem: Tic Tac Toe Game Logic

  • by

In Amazon SDE 2 interviews, technical rounds often test how well you can write production-ready code while solving real problems. A popular example is building a Tic Tac Toe engine. At first, it may sound like a simple game, but in reality, it measures your ability to design clean, modular, and scalable solutions while handling edge cases such as invalid inputs, alternating turns, and detecting winners or draws.

What Interviewers Expect

When asked to implement a Tic Tac Toe engine, you are not just writing a working version of the game. You should focus on:

  • Code design and scalability: organizing the logic so it can be extended for bigger boards or new rules.

  • Validation and error handling: ensuring moves are within range and that the chosen cell is empty.

  • Efficient data structures: choosing the simplest representation while keeping access and updates fast.

Ideal Answer

The engine must maintain the board state, handle player turns, validate moves, and check for win or draw conditions. A clean approach is to use a TicTacToe class with methods for making moves, checking winners, and exposing the game state. A 2D list works well to represent the board. Input validation ensures moves are safe, and variables track both the current player and whether the game has ended.

Python Implementation

from typing import List, Optional

class TicTacToe:
def __init__(self, size=3):
self.board = [[” for _ in range(size)] for _ in range(size)]
self.size = size
self.curr_player = ‘X’
self.winner = None
self.game_over = False
self.move_cnt = 0

def make_move(self, row: int, col: int) -> bool:
if self.game_over:
print(“Game already over.”)
return False
if row < 0 or row >= self.size or col < 0 or col >= self.size:
print(“Invalid move: out of bounds.”)
return False
if self.board[row][col] != ”:
print(“Invalid move: cell occupied.”)
return False

self.board[row][col] = self.curr_player
self.move_cnt += 1

if self.check_winner(row, col):
self.winner = self.curr_player
self.game_over = True
print(f”Player {self.curr_player} wins.”)
elif self.move_cnt == self.size * self.size:
self.game_over = True
print(“Game is a draw.”)
else:
self.curr_player = ‘O’ if self.curr_player == ‘X’ else ‘X’
return True

def check_winner(self, row: int, col: int) -> bool:
mark = self.curr_player
size = self.size
if all(self.board[row][c] == mark for c in range(size)):
return True
if all(self.board[r][col] == mark for r in range(size)):
return True
if row == col and all(self.board[i][i] == mark for i in range(size)):
return True
if row + col == size – 1 and all(self.board[i][size – 1 – i] == mark for i in range(size)):
return True
return False

def get_board(self) -> List[List[str]]:
return self.board

def get_winner(self) -> Optional[str]:
return self.winner

def is_over(self) -> bool:
return self.game_over

def print_board(self):
for row in self.board:
print(‘ | ‘.join(cell if cell else ‘ ‘ for cell in row))
print(‘-‘ * (self.size * 4 – 3))

Example Walkthrough

game = TicTacToe()
game.make_move(0, 0) # X
game.make_move(1, 1) # O
game.make_move(0, 1) # X
game.make_move(1, 0) # O
game.make_move(0, 2) # X wins
game.print_board()
print(“Winner:”, game.get_winner())
print(“Game Over:”, game.is_over())

Result:

X | X | X
———–
O | O |
———–
| |
  • Winner: X

  • Game Over: True

Design Choices

  • 2D list: keeps the board representation simple and efficient.

  • Class-based design: groups logic and allows for easy extension, such as larger boards or new game rules.

  • Validation: prevents invalid moves and ensures correct game flow.

  • Winner check: optimized to check only the row, column, and diagonals affected by the last move.

This structure keeps the engine maintainable and future proof.

Why This Matters

A Tic Tac Toe implementation may look like a toy problem, but for Amazon SDE 2 interviews, the focus is on your problem solving style. Interviewers are watching how you plan, write clean code, and think about long term maintainability under time pressure.

How CareerXcelerator Prepares You

CareerXcelerator gives you more than just random practice questions. It offers real-world coding challenges and structured problem solving exercises tailored for SDE 2 interviews. Beyond that, it provides mock interviews that feel just like the real experience, where you solve problems live, explain your reasoning, and face time constraints. After each practice session, you get in depth feedback on your logic, coding style, and communication.

By combining targeted exercises with interview simulation, CareerXcelerator helps you not only learn solutions but also develop the confidence to perform in actual interviews at companies like Amazon.

You can join our WhatsApp group where we share daily job openings, organize mock interview sessions, and provide mentor guidance. This is the fastest way to prepare smartly, stay updated with opportunities, and take a confident step toward your dream job.

Leave a Reply

Your email address will not be published. Required fields are marked *