Microsoft Interview Question
Software Engineer / Developers@ahj this is really correct answer
A[i][j] = A'[M-j-1][i]
one quick question however...
apart from creating and observing this pattern... is there any other way.. algebric to reach this answer
A[i][j] = A'[M-j+1][i] is wrong
Here M = 3, and N= 2
using above relation A[0][0] =A'[4][0]
So,
A[i][j] = A'[M-j][i] is correct answer
It is like cycle detection algorithm:
Lets say in initial matrix (row,colun) -> (i,j)
So now index in array = COLS * row + column.
Now the same index after transition will be index = ROW*col + row.
That is in the trasitioned matrix row and columns are interchanged and also the new matrix order is also reversed.
Ex (1,2,3,4,5,6) becomes now (1,4,2,5,3,6) (Represented in a 2 X 3 matrix)
But from the question these are again reversed.
So ow the solution becomes
newi = (COL - ROW*col + row) / COL
newj = ( COL - ROW*col + row.) / COL
This logic might be complex , If some one has a better logic please share
It is like cycle detection algorithm:
Lets say in initial matrix (row,colun) -> (i,j)
So now index in array = COLS * row + column.
Now the same index after transition will be index = ROW*col + row.
That is in the trasitioned matrix row and columns are interchanged and also the new matrix order is also reversed.
Ex (1,2,3,4,5,6) becomes now (1,4,2,5,3,6) (Represented in a 2 X 3 matrix)
But from the question these are again reversed.
So ow the solution becomes
newi = (COL - ROW*col + row) / COL
newj = ( COL - ROW*col + row.) / COL
This logic might be complex , If some one has a better logic please share
- ahj August 29, 2010