Interview Question

Anonymous Interview New York, NY

Given a 2D

rectangular matrix of boolean values, write a function which returns whether or not the matrix is the same when rotated 180 degrees. Additionally verify that every boolean true is accessible from every other boolean true if a traversal can be made to an adjacent cell in the matrix, excluding diagonal cells. That is , (x , y ) can access the set [ ( x + 1 , y ) , ( x - 1 , y ) , (x , y - 1 ) , (x , y + 1 ) ] For example, the matrix { { true , false } , { false , true } } should not pass this test.
Answer

Interview Answer

4 Answers

0

if the matrix A is
a11, a12
a21, a22
after 180 rotation
a22, a21
a12, a11
so a11 == a22 and a12 == a21
function is BOOL isSame = (a11==a22) && (a12==a21)
done.

David C on Jun 17, 2013
0

public static boolean isMatrixEqualToFlip(boolean[][] matrix) {
        if (matrix==null || matrix.length == 0 || matrix[0].length == 0) {
            return true;
        }
        int rowlen = matrix[0].length;
        int highInd = matrix.length/2;
        int lowInd = highInd - 1 + (matrix.length % 2);
        System.out.println("rowlen: " + rowlen + " high: "+ highInd + " low: " + lowInd);
        while (lowInd >= 0) {
            System.out.println("high: " + highInd + " lowInd: " + lowInd);
            for(int i=0; i < rowlen; i++) {
                System.out.println("Compare " + matrix[highInd][i] + " to " + matrix[lowInd][rowlen - 1 - i]);
                if (matrix[highInd][i] != matrix[lowInd][rowlen - 1-i]) {
                    return false;
                }
            }
            lowInd--;
            highInd++;
        }
        return true;
    }

Ben H on Jul 26, 2013
0

def rotate180(mtx):
    col=mtx
    col.reverse()
    for row in col:
        row.reverse()

    print col

Anon on Jul 28, 2013
0

If the matrix is:

true, false
false, true

90 degree rotation would be:

false, true
true, false

180 degree rotation would be:

true, false
false, true

If we define the matrix as:

a11, a12
a21, a22

Then the solution would be:

boolean isMatch = !(a11 && a12) && !(a11 && a21) && !(a12 && a22) && !(a21 && a22);

kaetem on May 9, 2014

Add Answers or Comments

To comment on this question, Sign In with Facebook or Sign Up