Winning Pattern Detection This was a question asked to me before setting up an interview.
You are given a game board that contains a four by four (4x4) array of pieces. The pieces can be one of six shapes and can be one of six colors. 

If the board contains 4 pieces of the same shape and same color, the board contains a winning pattern. 

public enum Color 
{ 
Red, 
Blue, 
Green, 
Yellow, 
Black, 
Purple 
} 

public enum Shape 
{ 
Square, 
Triangle, 
Circle, 
Star, 
Pentagon, 
Octagon 
} 

public class Piece 
{ 
public Color color; 
public Shape shape; 

public bool Equals(Piece compareTo) {}; 
} 

public class Board 
{ 
public Piece[,] position = new Piece[4,4]; 

public void Board(){ /*completely builds the board*/}; 
public void MakeRandomBoard(){}; 
public piece GetPiece(int x) {}; 
public bool IsWinner() {}; 
} 


Write the code to detect when a winning pattern is present in a board.

Answer :

bool isWinner() {
    HashMap &lt;Pair&lt;Color, Enum&gt;, int&gt; pieceCount;
    for(int row = 0; row &lt; 4; row++) {
        for(int col =0; col &lt; 4; col++) {
            Piece p = GetPiece(row, col); // Note: Sig is different than Given.
            Pair &lt;Color, Enum&gt; pair(p.Color, p.Enum);
            if (pieceCount.Contains(pair)) {
                pieceCount[pair]++;
                if (pieceCount[pair] &gt;= 4) return true;
            } else {
                pieceCount[pair] = 1;
            }
        }
    }
    return false;
}


The question was to come up with 3 different ways to solve this and list the pros and cons for each solution. This is one of the solution that I was able to do.
Please give your inputs on the code.
Another method would be to check the board as each piece is added. This has an advantage of real time checking, but requires looping through the whole board.  Also you could use a Dictionary&lt;Piece, int&gt; to keep track of which pieces are added and check that instead of the board array.  Using the ContainsKey eliminates extra looping but requires an extra collection.