Yatra.com Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
3
of 3 vote

Transpose
    for i in [0, n)
        for j in [0, n)
            if ( i < j )
                swap( M[i][j], M[j][i] )

- mag January 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can be done by swapping elements one by one of the 1st row and 1st column and then recursively passing the rest of the matrix (excluding 1st row and 1st column).

// rs -row start
// cs - column start
// re - row end
// ce - col end

  3 void transposeMat(int** a, int rs, int cs, int re, int ce) {
  4     int rows = re-rs;
  5     if (rows==0)
  6         return;
  7 
  8     int i, t;
  9     for (i=rs; i<=rows; i++) {
 10         t = a[i][cs];
 11         a[i][cs] = a[cs][i];
 12         a[cs][i] = t;
 13     }
 14 
 15     transposeMat(a, rs+1, cs+1, re, ce);
 16 }

- sachin January 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For square matrix:

void Transpose()
        {
            for (int i = 0; i < rowSize; i++)
            {
                for (int j = i; j < rowSize; j++)
                {
                    int temp = matrix[i, j];
                    matrix[i, j] = matrix[j,i];
                    matrix[j, i] = temp;
                }
            }

        }

- Anonymous January 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

when a matrix is non-square, we might run into loops when swapping the elements..
the following soln works:
1. assume matrix is stored as 1D array
2. for each element in the resulting (transposed) matrix, say its index is 'k', find the index of a corresponding element in the original matrix, say it's 'j'
3. if j > k then swap(a[j], a[k])
4. otherwise we should "follow the loop" since element in the location 'j' has
already been swapped

I hope my obscure explanations are understandable ))

here is the code:

// in:  n rows; m cols
// out: n cols; m rows
void matrix_transpose(int *a, int n, int m) {
    int i, j;
    printf("\nin: %d x %d\n", n, m);
    for(i = 0; i < n; i++) {
        for(j = 0; j < m; j++) 
            printf("%d ", a[i*m + j]);
        printf("\n");
    }
    for(int k = 0; k < n*m; k++) {
        int idx = k;
        do { // calculate index in the original array
            idx = (idx % n) * m + (idx / n);
        } while(idx < k);
        std::swap(a[k], a[idx]);
    }
    printf("\nout: %d x %d\n", m, n);
    for(i = 0; i < m; i++) {
        for(j = 0; j < n; j++)
            printf("%d ", a[i*n + j]);
        printf("\n");
    }
}

int main() {
    int n = 2, m = 8; // n rows, m cols
    int a[] = {1,1,1,1, 2,2,2,2, 3,3,3,3, 4,4,4,4};
    matrix_transpose(a, n, m);
    return 1;
}

- 111 January 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you explain more about this block?

do { // calculate index in the original array
            idx = (idx % n) * m + (idx / n);
        } while(idx < k);

- Peter August 05, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I used transform and coding for this problem.
When the matrix is a square, it's a trivial job to transform. i.e. by swapping lower and upper triangular matrix.

When the matrix is a non square, I'll get the square matrix out of it, i.e. if it is a '4X2' matrix, I'll apply the above method for '2X2' and I will be left to transform 3rd and 4th row. So you can interchange the row and column number to get the solution

Here is a code which implements the same

#include<iostream>

using namespace std;

const int m = 3; //Number of rows
const int n = 10; //Number of columns

int main()
{
int a[10][10] = {
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 },
{ 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 }
};

int i, j;

cout << "MATRIX A : \n";

for( i = 0; i < m; i++ )
{
for( j = 0; j < n; j++ )
cout << a[i][j] << "\t";
cout << endl;
}

// Transpose begins here

int index, m_index = 0, n_index = 0;

if( m == n )
index = n;
else if( m < n )
m_index = index = m;
else
n_index = index = n;


for( i = 0; i < index-1; i++ )
{
for( j = i+1; j < index; j++ )
{
int temp = a[i][j];
a[i][j] = a[j][i];
a[j][i] = temp;
}
}

if( m_index == 0 ) // implies n < m
{
for( i = n; i < m; i++ )
for( j = 0; j < n; j++ )
{
cout << " i : " << i << " j : " << j << endl;
a[j][i] = a[i][j];
a[i][j] = 0;
}
}

else if( n_index == 0 ) // implies m < n
{
for( i = m; i < n; i++ )
for( j = 0; j < m; j++ )
{
a[i][j] = a[j][i];
a[j][i] = 0;
}
}

cout << "\nTRANSPOSE OF A : \n";

for( i = 0; i < n; i++ )
{
for( j = 0; j < m; j++ )
cout << a[i][j] << "\t";
cout << endl;
}

return 0;
}

- Supreeth June 24, 2014 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More