Apple Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
6
of 6 vote

public class TicTacToe {

	static int n = 3;
	static char[][] board = new char[n][n];
	private static char currentPlayer='X';

	public static void main(String[] args) {
		initializeBoard(n);
		placeMark(0,0);
		printBoard();
		checkForWin();
		changePlayer();

		placeMark(0,1);
		printBoard();
		checkForWin();
		changePlayer();
		
		placeMark(1,1);
		printBoard();
		checkForWin();
		changePlayer();
		
		placeMark(2,0);
		printBoard();
		checkForWin();
		changePlayer();
		
		placeMark(2,2);
		printBoard();
		checkForWin();
		changePlayer();
		
		placeMark(3,0);
		printBoard();
		checkForWin();
		changePlayer();
		
		placeMark(3,1);
		printBoard();
		checkForWin();
		changePlayer();
		
		printBoard();
		isBoardFull(n);
		checkForWin();
	}

	public static void checkForWin() {

		if(checkRowsForWin() || checkColumnsForWin() || checkDiagonalsForWin())
		{
			System.out.println("Player :-"+currentPlayer+" Won the game");
			System.exit(0);
		}

	}

	private static boolean checkRowsForWin() {
		for (int i = 0; i < n; i++) {
			if (checkRowCol(board[i][0], board[i][1], board[i][2]) == true) {
				return true;
			}
		}
		return false;
	}

	private static boolean checkColumnsForWin() {
		for (int i = 0; i < n; i++) {
			if (checkRowCol(board[0][i], board[1][i], board[2][i]) == true) {
				return true;
			}
		}
		return false;
	}

	private static boolean checkDiagonalsForWin() {
		return ((checkRowCol(board[0][0], board[1][1], board[2][2]) == true) || (checkRowCol(
				board[0][2], board[1][1], board[2][0]) == true));
	}
	
	private static boolean checkRowCol(char c1, char c2, char c3) {
		        return ((c1 != '-') && (c1 == c2) && (c2 == c3));
		    }


	private static boolean isBoardFull(int n2) {
		boolean isFull = true;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if (board[i][j]=='-') {
					isFull = false;
				}
			}
		}
		return isFull;
	}

	private static void printBoard() {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				System.out.print(board[i][j]+" ");
			}
			System.out.println();
		}
	}

	private static void initializeBoard(int n) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				board[i][j] = '-';
			}
		}
	}

	
	    public static void changePlayer() {
	    	System.out.println("Player changed");
	        if (currentPlayer == 'X') {
	            currentPlayer = 'O';
	        }
	        else {
	            currentPlayer = 'X';
	        }
	    }
	    public static boolean placeMark(int row, int col) {
	         
	        if ((row >= 0) && (row < n)) {
	            if ((col >= 0) && (col < n)) {
	                if (board[row][col] == '-') {
	                    board[row][col] = currentPlayer;
	                    return true;
	                }
	            }
	        }
	         
	        return false;
	    }
	}

- Vir Pratap Uttam May 18, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Algorithm @ en.wikipedia.org/wiki/Tic-tac-toe

- codemonkey March 21, 2012 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More