Interview Question
Country: United States
the odds of an array address starting from 0 is 0...You need to know the address of a[0][0] to normalize the address...even with this information, you are still dead without the type. Your calculations assume this is a char array, changing the type will change the calculations.
For example assume int (size 4).
a[5][5]=N*4*5+5*4=1000 => N=49
a[7][9]=49*7*4+9*4=1408
All in all I agree with your approach, but more agree with everyone else...you do not want to work here
I also got curious and tried, my logic was right, its 24th element from [5][5] with int as 4 bytes it is +96 address of a[5][5]
Tried a C program for same and this is the address which verfies it
Address of a[5][5] is 1821490124
Address of a[7][9] is 1821490220
The interviewer is testing your understanding of the lower-level implementation of these things. There's no way of knowing how to proceed without knowledge of the following:
1. Is the matrix notation implemented with a contiguously allocated backing array such that
a[][] = new matrix[5][5]; is structurally similar to a[] = new array[25]; but supports special matrix manipulation operators at the higher level?
2. What is the size of the elements in the matrix, including any alignment overhead etc.
3. Assuming address 0 is reserved for 'null' and the size of the elements has been given. Does the derived address seem valid or does it need further cross-examination.
1402
- Anonymous August 06, 2014Assuming a[y][x] = N*y+x (starting from 0)
a[5][5] = N*5+5 = 1000 => N=995/5=199
a[7][9] = N*7+9 = 199*7+9 = 1402