Interview Question


Country: United States




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

First add a "fence" around the original grid, so that the original grid is surrounded by 2 rows each on the top & bottom, and 2 cols each on the left & right. Then set the value for these fence positions to be -1. That way you can check the opponent cell and landing cell without worrying about array out of bounds. After that, just do DFS.
The following code is untested. It also seems quite long, which would be hard to implement during an actual interview.

import java.util.*;

class MinSteps {
	static class Position {
		int r;
		int c;
		public Position(int r, int c) {
			this.r = r;
			this.c = c;
		}
		public int hashCode() {
			return 1001*r + c;
		}
		public boolean equals(Position p) {
			return r == p.r && c == p.c;
		}
	}

	static int maxJumps(int[][] grid, int r, int c) {
		int numRows = grid.length;
		int numCols = grid[0].length;
		// create new grid that contains an extra 'fence'
		// with value -1 to help check for boundary conditions
		int[][] newGrid = new int[numRows+4][numCols+4];
		for(int i=0; i<numRows+4; i++) {
			for(int j=0; j<numCols+4; j++) {
				if(i <= 1 || i >= numRows+2 || j <= 1 || j >= numCols+2)
					newGrid[i][j] = -1;
				else newGrid[i][j] = grid[i-2][j-2];
			}
		}
		r++;
		c++;
		HashSet<Position> seen = new HashSet<Position>();
		seen.add(new Position(r, c));
		return helper(grid, r, c, seen);
	}

	static int helper(int[][] grid, int r, int c, HashSet<Position> seen) {
		int max = 0;
		for(int k=0; k<4; k++)
			max = Math.max(max, helper2(grid, r, c, seen, k));
		return max; 
	}

	static int helper2(int[][] grid, int r, int c, HashSet<Position> seen, int direction) {
		int orig = grid[r][c];
		int opponent = (orig == 1 ? 2 : 1);
		int over;
		int land;
		Position p;
		if(direction == 0) {
			over = grid[r-1][c-1];
			land = grid[r-2][c-2];
			p = new Position(r-1, c-1);
		} else if(direction == 1) {
			over = grid[r-1][c+1];
			land = grid[r-2][c+2];
			p = new Position(r-1, c+1);
		} else if(direction == 2) {
			over = grid[r+1][c-1];
			land = grid[r+2][c-2];
			p = new Position(r+1, c-1);
		} else {
			over = grid[r+1][c+1];
			land = grid[r+2][c+2];
			p = new Position(r+1, c+1);
		}
		if(over != opponent || land != 0 || seen.contains(p))
			return 0;
		seen.add(p);
		int jumps = 1 + helper(grid, p.r, p.c, seen);
		seen.remove(p);
		return jumps;
	}
}

- Sunny November 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Using a HashSet like you are to prevent revisiting Positions will not work since it will prevent certain valid paths. It will fail on inputs like:

1, 0, 0, 0, 0, 0, 0
0, 2, 0, 2, 0, 2, 0
0, 0, 0, 0, 0, 0, 0
0, 2, 0, 2, 0, 2, 0
0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0

when it should be able to go (0,0) -> (2, 2) -> (4, 0) -> (6, 2) -> (4, 4) -> (2, 2) -> (0, 4)

- zortlord December 02, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

That's a good point and I forgot about that. Instead of setting variable p to each "landing cell", I should have set it to the "opponent cell" instead.

So for the first if(direction == 0) I should set have set:
p = new Position(r-1, c-1);

I will edit the code above accordingly. Thanks for catching and let me know if you spot any other error.

- Sunny December 02, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

So, basically figure out how many jumps can be done with given a game of checkers moving a single piece (a 'king'). This is roughly O(n^2) where n is the dimension of the board (8 in this case). More specifically, completing this via DFS using backtracking.

private static final int DIM = 6;

public static int getNumberOfJumps(int[][] board, Position start){
	if(board == null){
		throw new NullPointerException();
	}
	if(board.length < 3 || board.length >= DIM || board[0].length < 3 || board[0].length >= DIM){
		throw new IllegalArgumentException();
	}
	if(start == null){
		throw new NullPointerException();
	}
	if(start.x < 0 || start.x >= DIM || start.y < 0 || start.y >= DIM){
		throw new IllegalArgumentException();
	}

	Worker worker = new Worker(board);
	worker.search(start);
}

class Position{
	int x;
	int y;
	Position(int x, int y){
		this.x = x;
		this.y = y;
	}
}

class Worker{
	private static final Position[] MOVE_DIRECTIONS = new Position[]{
		new Position(-1,-1),
		new Position(-1,1),
		new Position(1,-1),
		new Position(1,1)
	};
	
	int[][] board;
	boolean[][] jumped;

	Worker(int[][] board){
		this.board = board;
		this.jumped = new boolean[board.length][board[0].length];
	}

	private boolean isValidMove(Position opponent, Position end){
		//destination within the board
		if(end.x < 0 || end.x >= DIM || end.y < 0 || end.y >= DIM)
			return false;
		//destination is open
		if(this.board[end.x][end.y] != 0)
			return false;
		//jumping over an opponent
		if(this.board[opponent.x][opponent.y] != 2)
			return false;
		return true;
	}

	private Position[] genValidMoveData(Position start){
		ArrayList<Position> working = new ArrayList<Position>();
		for(Position direction : MOVE_DIRECTIONS){
			Position opponent = new Position(start.x + direction.x, start.y + direction.y);
			Position end = new Position(opponent.x+direction.x, opponent.y+direction.y);
			if(isValidMove(opponent, end) && !jumped[opponent.x][opponent.y]){
				working.add(opponent);
				working.add(end);
			}
		}
		return working.toArray();
	}
			
	int search(Position start){
		int max = 0;
		Position[] moves = genValidMoveData(start);
		for(int i = 0; i < moves.length; i+=2){
			Position opponent = moves[i];
			Position dest = moves[i+1];
			this.jumped[opponent.x][opponent.y] = true;
			int val = search(dest)+1;
			if(val > max){
				max = val;
			}
			this.jumped[opponent.x][opponent.y] = false;
		}
		return max;
	}
}

- zortlord December 02, 2014 | 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