Adobe Interview Question
Software Engineer / DevelopersI guess your approach is fine, but would you mind explaining the answer a bit with a simple example for others to follow. thank you.
Thanks for the suggestion masamushi!
Following is the explanation..
First the iterative approach which is pretty simple.
Start at the beginning ( a(1,1)) of the diagonal matrix,and swap every element on the 1st row with the corresponding element on
the 1st column. Then do the same thing with the next element on the diagonal matrix viz. a(2,2),and so on. Once you reach the last
element on the diagonal matrix ,you need not do anything - the matrix is already transposed at this stage.
For the recursive approach, we can have the function transpose(A,k,n) where A is the matrix, k the current location on the diagonal matrix,
and N the order of the matrix (the square matrix)
Then the following code works similar to the iterative approach
transpose(A,k,N){
// if reached end of diag matrix,return
if(k==N)
return;
// interchange the elements on the k-th row with the corresponding elements of the k-column starting at a(k,k)
for(i = k+1;i <= N;i++)
{
// swap a[i,k] with a [k,i]
}
// recursively call the transpose function on the smaller square matrix starting at k+1
transpose(A,k+1,N);
}
Assumption : Matrix is a square matrix.
@Hello - Non-Square matrix do hava a Transpose matrix associated to it.
void Transpose(int input[][] matrix, int iterationCount){
if(iterationCount == 0) return;
for(int i= 0 ; i< iterationCount ;i++){
Swap(input[iterationCount][i],input[i][iterationCount]);
}
Transpose(matrix, iterationCount-1);
}
My approach:
- random September 11, 2009transpose(A,k,N){
if(k==N)
return;
for(i = k+1;i <= N;i++)
{
// swap a[i,k] with a [k,i]
}
transpose(A,k+1,N);
}