Interview Question


Country: India
Interview Type: Written Test




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

for(i=0;i<row;i++)
	{
		for(j=0;j<col;j++)
		{
			if(i%2==0)
				b[j][i]=a[i][j];
			else
				b[j][i]=a[i][col-j-1];
		}
	}

- Anonymous October 31, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class RowColum {

static void updateArray(int size){
int n = size, count=1;
int arr[][] = new int[n][n];
for(int i = 0; i < n; i++){
if(i%2==0){
for(int j=0; j<n; j++){
arr[j][i] = count++;
}
}
else{
for(int j = n-1; j >-1; j--){
arr[j][i] = count++;
}
}
}

for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
System.out.print(arr[i][j] +"\t");
}
System.out.println();
}
}

public static void main(String args[]){
updateArray(3);
}
}

- PTR October 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.io.*;

public class snakeMatrix{

public static void main(String[] args)
{
int totalRows,totalCols;
int row,col;
System.out.println("Enter total Number of rows or cols :");
totalCols=totalRows=5;//can be changed

int a[][]=new int[totalRows][totalCols];
for(row=0;row<totalRows;row++)
{
int x=10*row;
for(col=0;col<totalCols;col++)
{
a[row][col]=row+col+x;//generating random numbers
}
}

System.out.println("\n"+"\n");

for(row=0;row<totalRows;row++)
{
for(col=0;col<totalCols;col++)
{
//System.out.print(a[row][col]+"\t");
}
System.out.print("\n");
}

System.out.println("\n"+"\n");

for(col=0;col<totalCols;col++)
{
for(row=0;row<totalRows;row++)
{
if( ((col+2)%2) == 0)
System.out.print(a[row][col]);
else
System.out.print(a[totalRows-row-1][col]);
System.out.print("\t");
}
System.out.print("\n");
}

}
}

- asdf October 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

Transpose then swap 2 elements:

for(i=0; i<3; i++)
	for(j=i+1; j<3; j++) 
		swap(a[i][j], a[j][i]); //transpose

swap(a[0][1], a[2][1]);  //extra swap

- Urik's twin Cookie Monster (dead now) October 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Nevertheless, for this specific problem size (fixed problem),

It is simply a permutation of the 6 nondiagonal spots.
So you can hard code 7 writes/copies using 1 temp variable if you like.
No loops needed.

- Urik's twin Cookie Monster (dead now) October 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice.... +1

- Prakash October 29, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

ArrayList<Integer> a = new ArrayList<Integer>();

int row=mat.length;
int col=mat[0].length;
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
a.add(mat[i][j]);
}
}

int i=0;
int count=0;
for(int j=0;j<col;j++){
System.out.println("==> J:"+j);
if(j%2!=0){
for(i=2;i>=0;i--){
mat1[i][j]=a.get(count);
count++;
}
}
if(j%2==0){
for(i=0;i<=2;i++){
mat1[i][j]=a.get(count);
count++;
}
}

}

- Piyush February 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class matSum {


static int[][] mat =
{
{1,2,3},
{4,5,6},
{7,8,9}

};
static int[][] mat1 = new int[3][3];

public static void main (String args[]){
ArrayList<Integer> a = new ArrayList<Integer>();

int row=mat.length;
int col=mat[0].length;
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
a.add(mat[i][j]);
}
}

int i=0;
int count=0;
for(int j=0;j<col;j++){
System.out.println("==> J:"+j);
if(j%2!=0){
for(i=2;i>=0;i--){
mat1[i][j]=a.get(count);
count++;
}
}
if(j%2==0){
for(i=0;i<=2;i++){
mat1[i][j]=a.get(count);
count++;
}
}

}
}
}

- piyush88 February 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def swapMatrix(matrix):
row = len(matrix)
column = len(matrix[0])
mat_duplicate = [[0 for i in range(0, row)] for j in range(0, column)]

for i in range(0, row):
for j in range(0, column):
if i % 2 != 0:
mat_duplicate[column - j - 1][i] = matrix[i][j]
else:
mat_duplicate[j][i] = matrix[i][j]

return mat_duplicate

- Kathiresh Muthusamy October 16, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def swapMatrix(matrix):
    row  = len(matrix)
    column = len(matrix[0])
    mat_duplicate = [[0 for i in range(0, row)] for j in range(0, column)]

    for i in range(0, row):
        for j in range(0, column):
            if i % 2 != 0:
                mat_duplicate[column - j - 1][i] = matrix[i][j]
            else:
                mat_duplicate[j][i] = matrix[i][j]

    return mat_duplicate

- Kathiresh Muthusamy October 16, 2019 | 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