Microsoft Interview Question for Software Engineer / Developers


Country: India




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

start with simple solutions -
1) we can use multi dimentional matrix with arrays in C, where each element indicates position of piece as 0 or 1. you can even implement one dimentional array and use it as 2 dimentional by using row size.
2) In Java we can use arrayList also for the same..
As it is 8*8 array, you dont need any advanced DS to lookup or insert into it..

- nil_dream June 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

struct chessBoard {
    bool king:1;
    bool queen:1;
    bool bishop:1;
    bool knight:1;
    bool rooks:1;
    bool pawn:1;
    bool color:1;       
       
};

struct chessBoardSnapShot[8][8];

We can use this the structure to represent each cell of chess board and the 2 dimentional array to represent the chess board.

- ssaurabh June 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
2
of 4 vote

We can take two array[16] of bytes each contains coordinates of each pieces.
We store coordinates of black pieces in first array and white ones on second one.
Coordinate of each piece can be stored in one byte value:
0, 0 => 0x00
0, 1 =>0x01
1, 0 => 0x10
...
7, 6 => 0x76

- Atefeh June 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What if the item not in the board?0x99?

- Udhay June 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

For the items that are removed, set the value to 0xFF

- Atefeh June 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I like this solution. Only 32 bytes needed. If you were insane about compressing things you can store every position with just 6 bits too, reducing memory use further to 24 bytes. The extra complexity's probably not worth it though.

- Anonymous June 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

We can further reduce the space to 24 bytes. Lets see how.
There can be only 8 information: pawn,king,queen,bishop,knight,rook,no piece[empty] & color of piece.
So, we need 3 bits to represent to represent these information.
We can design a 8x8 chessboard with each slot occupying exactly 3 bits using bit-field[can represent range from 0 to 7].So, the space reduces to 64x3bits=24bytes.

- Aashish June 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You are assuming that only 16 pieces( i think 8 soldiers) can be on a board.But the soldiers can be converted into queen.So at one instance maximum 9 queens on board?..

or am i missing anything here?

- Raghavan June 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@shondik: you need 4 bits per square. The color of the piece is in combination with the other attributes. In other words, the distinct possibilities are (king, black), (king, white), (queen, black), (queen, white);, etc.

- eugene.yarovoi June 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why we need a fix size array? Why we need to save removed items?

- zprogd June 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think we can accomplish this using 32 bytes.Since each byte has 8 bits, we can use
1. The Least significant 6 bits to store the location of a piece 2. The 7th bit to identify if it is present or not on the board.
3. The 8th bit can be used to specify if it is a black piece or a white piece.

Please correct me if Im wrong..or if I missed something here.

- Pavan Dittakavi June 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This solution only tells address, and not the kind of piece

- Ashupriya August 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think it should be something close to listing 8x8 cells, and the occupany status of each cell. Also, for the cells being occupied, we should be able to tell which piece (pawn/bishop/king....etc) has occupied that cell.
Can't think of a simple primitive datastructure for this. Must have to be a combination of 2 or more datastructures.

- Learner June 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can take a 2D array[8x8], where each slot can contain one of the 7 things.
rook,bishop,king,queen,knight, pawn & empty.
So, the pieces can be represented by a single integer using bit vector.

- Aashish June 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

a 8x8 byte array would be more optimized in space. 1 byte can represent 255 different things. so its easy to represent all the 32 peices (16 for each player black and white) using 1 byte.

- nix June 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

One more thing needs to be added, the color information.

- Aashish June 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think We can also do it with 3 bits using bitmask. The range of values can range from 0 to 7 by using 3 bits.
So, the space is further reduced to: 64x3bits=24byte.

- Aashish June 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think there is little more to this problem. State of the chess board is not only which of the cells are occupied, but what that cell contain. So each cell would be representing the following
1) State ( Occupied/Empty)
2) Color of the Piece (White/Black)
3) Type of the Piece.

I think the problem will be focusing on, storing the state of the chessboard so that we can recover the state from the data structure at any point in time.

- Anonymous June 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Lets consider the requirements for a piece first and then can be taken care of for all the 32 pieces.
In a 2-D matrix we need following to represent a piece:
1. coordinates-(x,y): can be represented by 6 bits, 3 for x and 3 for y
2. If the piece is present or not: 1 bit

hence to represent one piece we need 7 bits and to represent 32 pieces we need 32*7 bits = 28bytes

DS can be an array, whose index will give us the type of piece.
Say, first 7 bits of a[0] would give us a black pawn status

- Anonymous June 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

3 bits for A-H, 3bits for 1-8 (co-ordinates A1 A2..... H7 H8)
1 bit for color 3bits for piece information (king queen bishop rook knight pawn empty)
.. total 4 bits...

hence 6*4 = 24 bits 3 bytes

- pankaj June 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

3 bits for A-H, 3bits for 1-8 (co-ordinates A1 A2..... H7 H8)
1 bit for color 3bits for piece information (king queen bishop rook knight pawn empty)
.. total 4 bits...

hence 6*4 = 24 bits = 3 bytes

- pankaj June 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Over and above everything else, one more bit is needed to note the parity of the board - whether the top left corner is black or white.

- sri June 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

cant it be char [2^64][8][8] array ?? we can create a character map for 8X8 elements like this
black (all odd numbers):
queen : 1
king : 3
pawns : 11

white (all even numbers):
queen ; 2
king : 4
pawns : 10

ideally it should be 2^64 - 1 positions if we are ignoring the nothing on chess board :) : ) !!

- softy July 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We need to store 4 things
1. color
2. presence
3. type of the coin
4. location

Locations range is (0,0) = 0x00 to (7,7) = 0x77 which need 6bits. That means 32 * 6bits = 24bytes (for storing the locations)

1 bit for color => 32 bits = 4 bytes
1 bit for presence => 32 bits = 4 bytes
3 bits for type of coin (000 - pawn, 001 - bishop, 010 - knight , 011 -rook, 100- queen, 101 - king) => 3*32 = 12 bytes

Total = 44 bytes

- prashaenator August 18, 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