Microsoft Interview Question
Software Engineer / DevelopersKhoa,
You would be knowing that any n-dimensional matrix (array) finally boils down to a sequential representation in memory.
Eg.:
a[0,0] a[o,1]
a[1,0] a[1,1]
would be stored as:
1-D Index 0 1 2 3
Address: 1000 1002 1004 1006
Item: a[0,0] a[0,1] a[1,0] a[1,1]
which is row major order.
Here, a[1,0] = a[2] (not literally, but in memory)
So, the Q was to write a general expression to represent this relation for both row and column major order representations of an array.
I hope this clarifies. Sorry, I typed the Q in a hurry so, it is ambiguous.
Lets say we have m x n matrix.
- takeiteasy June 24, 2008In row-major order we have
to access (i,j) we have ((i * no_of_col) + j).
we have ((i * n) + j) in our case.
In col-major order we have
to access (j,i) we have ((j * no_of_row) + i).
we have ((j * m) + i).
since (i,j)th element in row-major form is the same as (j,i) in col_major_order and m x n will now be n x m.
so equating we have ((i * n) + j) = ((j * m) + i)
we get ni + j = mj + i i.e i / j = (m - 1)/(n -1).
Can somebody please verify this.