Amazon Interview Question for SDE1s


Country: United States




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

Really? Amazon is surely running short on engineers.
TIP: consider n-passant rule in this one!

- Noobie March 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Noobie: good tip. but wouldn't it be necessary to know the move history for applying en-passant here ? (at least the immediate last move)

- VJ March 27, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ya you are right.

- Noobie March 27, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I didnt understood the question..can u explain more in detail?

- murali March 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can anyone please explain the question.. I didn't understand... or any other link relevant to this

- yogi.rulzz March 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

I did not even know what is board[8][8]. Chess knowledge is pre-required for this solution.

- outdoor March 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

and last I checked Chess was not part of computer science.
Yeah, right. Keep down voting. Chess will still not be part of Computer Science.

- EK MACHCHAR May 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is ridiculous. Tomorrow they will mandate that knowledge of kabbadi is essential for a coding interview.

- EK MACHCHAR May 05, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@VJ's code

2 possible inaccuracies:

1. forwardMove2 needs two conditions to be verified. Whether forwardMove == empty && forwardMove2 == empty.
Also, int forwardMove2 = isWhite? y+2:y-2; (instead of y++:y--)

2. Just checking isEmpty is not sufficient for DiagonalMoves. A diagonalMove for a white pawn is possible only if the position has a black piece. And likewise, a diagonalMove for a black pawn is possible only if the position has a white piece.

- DbgBrk March 15, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Haven't compiled the code. But, this should give a rough idea.

void possibleMoves( int board[8][8], x, y )
{

    bool isWhite;
    
    if( board[x][y] >= 1 && board[x][y] <= 9 )
        isWhite = true;
    else if( board[x][y] >= 11 && board[x][y] <= 19 )
        isWhite = false;
        
    if( isWhite && y == 1 )
    {
        if( board[x][y+1] == isEmpty() )
        {
            //cout << "Forward move : " << x << ", "  << y+1 << endl;
            if( board[x][y+2] == isEmpty() )
                cout << "Forward move : " << x << ", "  << y+2 << endl;
        }
    }

    if( !isWhite && y == 6 )
    {
        if( board[x][y-1] == isEmpty() )
        {
            //cout << "Forward move : " << x << ", "  << y-1 << endl;
            if( board[x][y-2] == isEmpty() )
                cout << "Forward move : " << x << ", "  << y-2 << endl;
        }
    }
   
    if( isWhite && y >= 1 && y <= 6 )
    {
        if( board[x][y+1] == isEmpty(x, y+1) )
            cout << "Forward move : " << x << ", "  << y+1 << endl;

		if( x >= 1 && x <=6 )
        {
            if( board[x-1][y+1] == isOccupiedByBlack(x-1, y+1) )
                cout << "Diagonal move : " << x-1 << ", "  << y+1 << endl;
            
            if( board[x+1][y+1] == isOccupiedByBlack(x+1, y+1) )
                cout << "Diagonal move : " << x+1 << ", "  << y+1 << endl;
        }
		else if( x == 0 )
		{
			if( board[x+1][y+1] == isOccupiedByBlack(x+1, y+1) )
				cout << "Diagonal move : " << x+1 << ", "  << y+1 << endl;
		}
		else if( x == 7 )
		{
			if( board[x-1][y+1] == isOccupiedByBlack(x-1, y+1) )
				cout << "Diagonal move : " << x-1 << ", "  << y+1 << endl;
		}
    }         
   
    if( !isWhite && y <= 6 && y >= 1 )
    {
		if( board[x][y-1] == isEmpty(x, y-1) )
            cout << "Forward move : " << x << ", "  << y-1 << endl;

        if( x >= 1 && x <=6 )
        {
            if( board[x-1][y-1] == isOccupiedByWhite(x-1, y-1) )
                cout << "Diagonal move : " << x-1 << ", "  << y-1 << endl;
            
            if( board[x+1][y-1] == isOccupiedByWhite(x+1, y-1) )
                cout << "Diagonal move : " << x+1 << ", "  << y-1 << endl;
        }
        else if( x == 0 )
        {
            if( board[x+1][y-1] == isOccupiedByWhite(x+1, y-1) )
				cout< "Diagonal move: " << x+1 << ", " << y-1 << endl;
        }
		else if( x == 7 )
		{
			if ( board[x-1][y-1] == isOccupiedByWhite(x-1, y-1) )
				cout << "Diagonal move: " << x-1 << ", " << y-1 << endl;
		}
    }
}            

bool isLegal( int a, int b )
{
    if( a < 0 || a > 7 || b < 0 || b > 7 )
        return false;
}


bool isEmpty( int a, int b )
{    
    if( isLegal ( a, b ) )
        if( (board[a][b] >= 1 && board[a][b] <= 9) || (board[a][b] >= 11 || board[a][b] <= 19) )
            return false;

	return true;
}

bool isOccupiedByBlack( int a, int b )
{
	if( isLegal ( a, b ) )
        if( board[a][b] >= 11 || board[a][b] <= 19 )
            return true;
	return false;
}

bool isOccupiedByWhite( int a, int b )
{
	if( isLegal ( a, b ) )
        if( board[a][b] >= 1 || board[a][b] <= 9 )
            return true;
	return false;	
}

- DbgBrk March 15, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

assuming we don't have the move history (for en-passant move), one of the solutions could be as follows:

void printPawnMoves(int[][] board, int x, int y) {
	assert (isWithinBounds(x,y));
	bool isWhite = false;
	
	if (board[x][y] > 0 && board[x][y] < 10) { isWhite = true; }
	else if (board[x][y] > 10 && board[x][y] < 20( { isWhite = false; }
	else { assert(true); }
	
	//each pawn can have a total of 4 possible moves
	int forwardMove = isWhite ? y++:y--;
	//forward moves
	if (isWithinBounds(x,forwardMove)) {
		if(isEmpty(board,x,forwardMove)) {
			cout << "(" + x + "," + forwardMove + ")\n";
		}
		//check if in the starting grid position
		if (y == (isWhite ? 1 : 6)) {
			int forwardMove2 = isWhite ? y++:y--;
			if(isEmpty(x,forwardMove2)) {
				cout << "(" + x + "," + forwardMove2 + ")\n";
			}
		}
	}
	
	//diagonal moves
	int diagLeftXMove = isWhite ? x++:x--;
	int diagRightXMove = isWhite ? x--:x++;
	if (isWithinBounds(diagLeftXMove,forwardMove) && !isEmpty(board,diagLeftXMove,forwardMove)) {
		cout << "(" + diagLeftXMove + "," + forwardMove + ")\n";
	}
	if (isWithinBounds(diagRightXMove,forwardMove) && !isEmpty(board,diagRightXMove,forwardMove)) {
		cout << "(" + diagRightXMove + "," + forwardMove + ")\n";
	}
	return;
}

bool isWithinBounds(int x, int y) {
	return ((x < 0 || x > 7 || y < 0 || y > 7) ? true:false);
}

bool isEmpty(int[][] board,int x, int y) {
	assert(isWithinBounds(x,y));
	return ((board[x][y] < 0 || board[x][y] > 19 || board[x][y] == 10) ? true:false);
}

- VJ March 27, 2013 | 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