Microsoft Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

public int[] modify(int[][] m)
{
	if(m==null||m.length==0||m[0].length==0)
	{
		return null;
	}
	
	int[] result=new int[m.length*m[0].length];
	int idx=0;
	for(int i=0;i<m.length;i++)
	{
		if(i%2==0)
		{
			for(int c=0;c<m[0].length;c++)
			{
				result[idx++]=m[i][c];
			}
			
		}else
		{
			for(int c=m[0].length-1;c>=0;c--)
			{
				result[idx++]=m[i][c];
			}
		}
	}
	return result;
}

- divm01986 July 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

public class Print2DAs1DArray {

	public static void main(String[] args) {
		
		int[][] arr = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
		boolean leftToRight = true;
		
		for(int i=0;i<arr.length;i++) {
			for(int j=0;j<arr[0].length;j++) {
				if(leftToRight) {
					System.out.print(arr[i][j] + " ");
				} else {
					System.out.print(arr[i][arr[0].length - j - 1] + " ");
				}
			}
			
			if(leftToRight) leftToRight = false;
			else leftToRight = true;
		}

	}

}

- CodeNameEagle July 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void readalternate(int arr[R][C])
{
	bool left=false,down=false,right=true;
	int i=0,j=0;
	while(i<R)
	{
		if(right)
		{
			while(j<C)
			{
				cout<<arr[i][j]<<" ";
				j++;
			}
			right=false;
			j=C-1;
			i++;
			left=true;
		}
		else if(left==true)
		{
			while(j>=0)
			{
				cout<<arr[i][j]<<" ";
				j--;
			}
			left=false;
			i++;
			j=0;
			right=true;
		}
	}

- anonymous July 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void readalternate(int arr[R][C])
{
bool left=false,down=false,right=true;
int i=0,j=0;
while(i<R)
{
if(right)
{
while(j<C)
{
cout<<arr[i][j]<<" ";
j++;
}
right=false;
j=C-1;
i++;
left=true;
}
else if(left==true)
{
while(j>=0)
{
cout<<arr[i][j]<<" ";
j--;
}
left=false;
i++;
j=0;
right=true;
}
}
}

- anonymous July 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.eb.corealgo;

public class MatrixPrint {
	
	public String  printreverse(int mat[][]){
		
		String res="";
		if(mat==null){
			
			throw new NullPointerException("Null MAT");
		}
		
		int rows=mat.length;
		
		int cols=mat[0].length;
	
		boolean isPrint=true;
		
		int row=0;
		
		while(isPrint){
			
			if(row<rows){
			
				for(int i=0;i<cols;i++){
					
					if(row%2==0){
						res+=mat[row][i]+" ";
					}
					else{
						
						res+=mat[row][cols-i-1]+" ";
					}
				}
				
				row++;
			}
			else{
				break;
			}
		}
		return res;
	}
	
	//test app
	
	public static void main(String args[]){
		
	
		int matrix[][]={
				{1,2,3,4,5},
				
				{6,7,8,9,10},
				
				{11,12,13,15,16}
		};
		
		MatrixPrint matPrint=new MatrixPrint();
		
		System.out.println(matPrint.printreverse(matrix));
			
	}
}

- shashi kumar July 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.eb.corealgo;

public class MatrixPrint {
	
	public String  printreverse(int mat[][]){
		
		String res="";
		if(mat==null){
			
			throw new NullPointerException("Null MAT");
		}
		
		int rows=mat.length;
		
		int cols=mat[0].length;
	
		boolean isPrint=true;
		
		int row=0;
		
		while(isPrint){
			
			if(row<rows){
			
				for(int i=0;i<cols;i++){
					
					if(row%2==0){
						res+=mat[row][i]+" ";
					}
					else{
						
						res+=mat[row][cols-i-1]+" ";
					}
				}
				
				row++;
			}
			else{
				break;
			}
		}
		return res;
	}
	
	//test app
	
	public static void main(String args[]){
		
	
		int matrix[][]={
				{1,2,3,4,5},
				
				{6,7,8,9,10},
				
				{11,12,13,15,16}
		};
		
		MatrixPrint matPrint=new MatrixPrint();
		
		System.out.println(matPrint.printreverse(matrix));
			
	}
}

- shashi kumar July 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void print2DArraySpirally(int arr[][]){
boolean flag = false;
for(int i = 0 ; i < arr.length; i++ ){
if(flag){
for(int j = arr[i].length - 1 ; j >= 0 ;j--){
System.out.print(arr[i][j]+" ");
}
}else{
for(int j = 0 ;j < arr[i].length ; j++){
System.out.print(arr[i][j]+" ");
}
}
flag = !flag;
System.out.println("");
}
}

- ultikhopdi July 16, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a=[[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
for i in range(len(a)):
if(i%2)==1:
j=2
while(j>-1):
print(a[i][j])
j=j-1
else:
j=0
while(j<3):
print(a[i][j])
j=j+1

- mohit July 16, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;

public class Test
{
public static void Print2DArray(long[,] arr)
{
int rowLength = arr.GetLength(0);
int colLength = arr.GetLength(1);
int iShift = 1;
int iColPos = 0;
for (int iRowPos = 0; iRowPos < rowLength; iRowPos++)
{
while (iColPos >=0 && iColPos < colLength)
{
Console.Write(string.Format("{0} ", arr[iRowPos, iColPos]));
iColPos += iShift;
}
iShift *= -1 ;
iColPos += iShift;
}
Console.Write(Environment.NewLine + Environment.NewLine);
}

public static void Main()
{
long[,] arr = new long[5, 4] {
{ 1, 2, 3, 4 },
{ 3, 4, 5, 6 },
{ 7, 8, 9, 10 },
{ 11, 12, 13, 14 },
{ 15, 16, 17, 18 }
};
Print2DArray(arr);
Console.ReadLine();
}
}

- Anonymous July 17, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;

public class Test
{
	public static void Print2DArray(long[,] arr)
	{
        int rowLength = arr.GetLength(0);
        int colLength = arr.GetLength(1);
		int iShift = 1;
		int iColPos = 0;
        for (int iRowPos = 0; iRowPos < rowLength; iRowPos++)
        {
            while (iColPos >=0 && iColPos < colLength)
            {
                Console.Write(string.Format("{0} ", arr[iRowPos, iColPos]));
                iColPos += iShift;
            }
            iShift *= -1 ;
            iColPos += iShift;
        }
        Console.Write(Environment.NewLine + Environment.NewLine);
	}
	
	public static void Main()
	{
		long[,] arr = new long[5, 4] { 
					{ 1, 2, 3, 4 }, 
					{ 3, 4, 5, 6 }, 
					{ 7, 8, 9, 10 }, 
					{ 11, 12, 13, 14 }, 
					{ 15, 16, 17, 18 } 
					};
		Print2DArray(arr);
        Console.ReadLine();
	}
}

- pras47 July 17, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i=n; (getting rows count
j=n; (getting columns count)

For (i=0; i<=n ;i++)
{
if(i&1==0)
{
for (j=0;j<n;j++)
{
print a[i][j];
}
else
{
for (j>n;j=0;j--)
print a[i][j];
}

}

- abhay40711cs July 18, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Matrix_Value_Print
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int m=sc.nextInt();
int n=sc.nextInt();
int arr[][]=new int[m][n];
int sarr[]=new int[m*n];
int t=0;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
arr[i][j]=sc.nextInt();
}
}
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(i%2==0)
{
sarr[t++]=arr[i][j];
}
else
{
for(int k=n;k>0;k--)
{
sarr[t++]=arr[i][k-1];
}
break;
}
}
}

System.out.println();
for(int i=0;i<sarr.length;i++)
{
System.out.print(sarr[i]+" ");
}
}
}

- Atique Ahmed(From India) July 18, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Program
    {
        // 3x3 2d Integer Array
        int [,] matrix = { {1,2,3}, {4,5,6}, {7,8,9} };

        static void Main(string[] args)
        {
            var p = new Program();
            var q = p.InOrder();
            var r = p.ReverseOrder();        
        }

        private int[] InOrder()
        {
            int size = matrix.GetLength(0) * matrix.GetLength(1);
            var result = new int[size];
            int index = 0;
            for (int o=0; o < matrix.GetLength(1); o++)
            {
                for (int i=0; i < matrix.GetLength(0); i++)
                {
                    result[index++] = matrix[o,i];
                }
            }
            return result;
        }

        private int[] ReverseOrder()
        {
            int size = matrix.GetLength(0) * matrix.GetLength(1);
            var result = new int[size];
            int index = 0;
            for (int o = matrix.GetLength(1)-1; o >= 0; o--)
            {
                for (int i = matrix.GetLength(0)-1; i >= 0; i--)
                {
                    result[index++] = matrix[o, i];
                }
            }
            return result;
        }
    }

- MonsterLlama July 18, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

vector<int> solve(vector<vector<int>> matrix) {
	vector<int> ans;
	for (int i = 0; i < matrix.size(); i++) {
		if (i%2) {
			ans.insert(ans.end(), matrix[i].rbegin(), matrix[i].rend()); 
		} else {
			ans.insert(ans.end(), matrix[i].begin(), matrix[i].end()); 
		}
	}
	return ans;
}

- Anonymous July 21, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) 
	{
		int[][] a = {{1,2,3},{4,5,6},{7,8,9}};
		int len = a.length;
		
		int t=0;
		int b = len-1;
		int dir =0;
		
		while(t<=b)
		{
			if(dir==0)
			{
				for(int i=0;i<len;i++)
				{
					System.out.print(a[t][i]+" ");
				}
				
				dir++;
				t++;
			}
			else if(dir==1)
			{
				for(int i=len-1;i>=0;i--)
				{
					System.out.print(a[t][i]+" ");
				}
				
				dir--;
				t++;
			}
		}
	}

- viksnap July 21, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <vector>
using namespace std;
void print(vector<vector<int> > &v){
    int M = v.size();
    int N = v[0].size();
    int i,j;
    for(i=0;i<M;i++){
        if(i%2==0){
            for(j=0;j<N;j++)
                cout << v[i][j] << " ";
        } else {
            for(j=N-1;j>=0;j--)
                cout << v[i][j] << " ";
        }

    }
}
int main(){
    int M,N;
    cin >> M >> N;
    vector<vector<int> > v(M,vector<int>(N));
    for(int i=0;i<M;i++){
        for(int j=0;j<N;j++){
            cin >> v[i][j];
        }
    }
    print(v);
    return 0;
}

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

def matrix_to_one_d_array(a):
	result = []
	for i in range(len(a)):
		if i % 2 is 0:
			result.append(a[i])
		else:
			q = a[i]
			q.reverse()
			result.append(q)
	return result

- montu July 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def matrix_to_one_d_array(a):
	result = []
	for i in range(len(a)):
		if i % 2 is 0:
			result.append(a[i])
		else:
			q = a[i]
			q.reverse()
			result.append(q)
	return result

- montu July 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def matrix_to_one_d_array(a):
result = []
for i in range(len(a)):
if i % 2 is 0:
result.append(a[i])
else:
q = a[i]
q.reverse()
result.append(q)
return result

- montu July 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def matrix_to_one_d_array(a):
	result = []
	for i in range(len(a)):
		if i % 2 is 0:
			result.append(a[i])
		else:
			q = a[i]
			q.reverse()
			result.append(q)
	return result

- montu July 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def matrix_to_one_d_array(a):
result = []
for i in range(len(a)):
if i % 2 is 0:
result.append(a[i])
else:
q = a[i]
q.reverse()
result.append(q)
return result

- montu July 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def matrix_to_one_d_array(a):
	result = []
	for i in range(len(a)):
		if i % 2 is 0:
			result.append(a[i])
		else:
			q = a[i]
			q.reverse()
			result.append(q)
	return result

- montu July 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Program
{
static void Main(String[] args)
{

int[,] a = new int[4, 4] { { 0, 1, 2, 3 }, { 4, 5, 6, 7 }, { 8, 9, 10, 11 }, {12,13,14,15} };



for (int i = 0; i < a.GetLength(0); i++)
{
if (i % 2 == 0)
{
for (int j = 0; j < a.GetLength(1); j++)
{
Console.Write(a[i, j] + " ");
}
}
else
{
for (int j = a.GetLength(1) - 1; j >= 0 ; j--)
{
Console.Write(a[i, j] + " ");
}

}

Console.WriteLine();
}


Console.ReadKey();
}
}

- Pradeep Devadiga August 02, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void Main(String[] args)
{
int[,] a = new int[4, 4] { { 0, 1, 2, 3 }, { 4, 5, 6, 7 }, { 8, 9, 10, 11 }, {12,13,14,15} };
for (int i = 0; i < a.GetLength(0); i++)
{
if (i % 2 == 0)
{
for (int j = 0; j < a.GetLength(1); j++)
{
Console.Write(a[i, j] + " ");
}
}
else
{
for (int j = a.GetLength(1) - 1; j >= 0 ; j--)
{
Console.Write(a[i, j] + " ");
}

}
Console.WriteLine();
}
Console.ReadKey();
}

- Pradeep Devadiga August 02, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

dasdasd

- asdas August 02, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def convertintosingle(mylist):
    myconvertedarray=list()
    for eachlist in mylist:
        if mylist.index(eachlist)%2==0:
            myconvertedarray.extend(eachlist)
        else:
            myconvertedarray.extend(eachlist[::-1])
    print myconvertedarray
convertintosingle(mylist=[[1,2,3],[4,5,6],[7,8,9],[10,11,12]])

- AnkitP August 06, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void Main(string[] args)
{
int[,] array = new int[,] {
{1,2,3 },
{4,5,6 },
{7,8,9 }
};

printArray(3, 3, array);
}
static void printArray(int m, int n, int[,] a)
{
int j = 0;
int k = 1;
for (int i = 0; i < 3; i++)
{
if (k % 2 == 0)
{
for (j=j-1;j >=0; j--)
{
Console.Write(a[i, j]);
}
}
else
{
j = j == 0 ? 0 : j - 1;
for (j=0; j <3; j++)
{
Console.Write(a[i, j]);
}
}
k++;
}

}

- Anonymous August 11, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void printArray(int m, int n, int[,] a)
{
int j = 0;
int k = 1;
for (int i = 0; i < 3; i++)
{
if (k % 2 == 0)
{
for (j=j-1;j >=0; j--)
{
Console.Write(a[i, j]);
}
}
else
{
j = j == 0 ? 0 : j - 1;
for (j=0; j <3; j++)
{
Console.Write(a[i, j]);
}
}
k++;
}

}

- Anonymous August 11, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void printArray(int m, int n, int[,] a)
        {
            int j = 0;
            int k = 1;
            for (int i = 0; i < 3; i++)
            {
                if (k % 2 == 0)
                {
                    for (j=j-1;j >=0; j--)
                    {
                        Console.Write(a[i, j]);
                    }
                }
                else
                {
                    j = j == 0 ? 0 : j - 1;
                    for (j=0; j <3; j++)
                    {
                        Console.Write(a[i, j]);
                    }
                }
                k++;
            }

        }

- Anonymous August 11, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

and

static void printArray(int m, int n, int[,] a)
{
int j = 0;
int k = 1;
for (int i = 0; i < 3; i++)
{
if (k % 2 == 0)
{
for (j=j-1;j >=0; j--)
{
Console.Write(a[i, j]);
}
}
else
{
j = j == 0 ? 0 : j - 1;
for (j=0; j <3; j++)
{
Console.Write(a[i, j]);
}
}
k++;
}

}

- Anonymous August 11, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void funMS_ReadAlternativeMatrix()
        {
            // Input
            Console.WriteLine("Matrix size: ");
            int size = Convert.ToInt32(Console.ReadLine());
            int[][] matrix = new int[size][];
            List<int> output = new List<int>();

            for (int row = 0; row < size; row++)
                matrix[row] = Array.ConvertAll(Console.ReadLine().Split(' '), Int32.Parse);

            // Logic
            for (int row = 0; row < size; row++)
            {
                bool flag = row % 2 == 0 ? true : false;
                int col = 0;
                for (int column = 0; column < size; column++)
                {
                    col = flag ? column : ((size - 1) - column);
                    output.Add(matrix[row][col]);
                }
            }

            // Output
            Console.WriteLine("Output: ");
            for (int i = 0; i < output.Count; i++)
                Console.Write(output[i] + " ");
            Console.ReadLine();
        }

- Pradeep Bhondawe August 19, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class MatrixReading {
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int[][] matrix= new int[3][3];
		int[] oneD= new int[9];
		int a=0;
		boolean rightToLeft=false;
		Scanner input = null;
		for(int i=0; i<=2;i++){
			for(int j=0;j<=2;j++){
				input = new Scanner(System.in); 
				System.out.println("Enter a number");
				matrix[i][j] = input.nextInt();
				//System.out.println(matrix[i][j]);
			}
		}
		input.close();
		
		for(int i=0; i<3;i++){
			if(rightToLeft){                   
				for(int j=2; j>=0;j--){
					  oneD[a] = matrix[i][j];
					  System.out.print(oneD[a]+" ");
					  a++;
				}
				rightToLeft=false;
			}else{
				for(int j=0; j<3;j++){
					  oneD[a] = matrix[i][j];
					  System.out.print(oneD[a]+" ");
					  a++;
				}
				rightToLeft = true;	
			}
		}
	}
}

- Srikanth Dukuntla August 23, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>

using namespace std;

int main() {
int n = 4;
int **arr = new int*[n];
for(int i=0; i<n; i++)
arr[i] = new int[n];

for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
arr[i][j] = i + (2*j) + 1;
}
}

cout << " Print matrix " << endl;
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}

cout << " Output " << endl;
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
if(!(i%2))
cout << arr[i][j] << " ";
else
cout << arr[i][n-j-1] << " ";
}
}

return 0;
}

- Kunal September 18, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>

using namespace std;

int main() {
int n = 4;
int **arr = new int*[n];
for(int i=0; i<n; i++)
arr[i] = new int[n];

for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
arr[i][j] = i + (2*j) + 1;
}
}

cout << " Print matrix " << endl;
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}

cout << " Output " << endl;
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
if(!(i%2))
cout << arr[i][j] << " ";
else
cout << arr[i][n-j-1] << " ";
}
}

return 0;
}

- kbkunalb September 18, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ReadMatrix {

public static void main(String[] args) {
int[][] array = {{1, 2, 3 , 11}, {4, 5, 9 ,12}, {7, 8, 9,13},{14,15,16,17}};
int[] output = new int[array.length*array.length];
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println(" ");
}
int left = 0;
int right = array.length - 1;
int arrayIndex = 0;
int index = 0;
String direction = "f";
while (left <= array.length -1) {
if (direction.equals("f")) {
index = 0;
while (index <= right) {
output[arrayIndex] = array[left][index];
arrayIndex++;
index++;
}
}
left++;
direction = "b";
if (direction.equals("b") && left<= array.length -1) {
index = array.length - 1;
while (index >= 0) {
output[arrayIndex] = array[left][index];
arrayIndex++;
index--;
}
}
left++;
direction = "f";
}

for (int j = 0; j < output.length; j++) {
System.out.print(output[j] + " ");
}
}
}

- tech.shekharsaxena October 07, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

python2.7

new_list = []
for x in range(len(list_l)):
	if x % 2== 0:
		new_list.extend(list_l[x])
	else:
		new_list.extend(list_l[x][::-1])
return new_list

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

public class Matrix {

public static void main(String[] args){
int arr[][] = {{1, 2, 3, 10},
{4, 5, 6, 20},
{7, 8, 9, 30}};
int i = 0;
int m = arr.length, n = arr[0].length;

while(i< m){
for(int r=0; r<n ; r++){
System.out.print( arr[i][r] + " ");
}
i++;
if( i == m)
break;
System.out.println();
for(int r= n -1; r >= 0 ; r--){
System.out.print( arr[i][r] + " ");
}
i++;
System.out.println();
}

}
}

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

static public void GetOrder(int[][] inp){
  
    int len = inp[0].length;
    for(int i=0;i<len;i++){
        for(int j=0;j<len;j++){
          System.out.print(inp[i][j]);
        }
        if(i != len-1){
          i++;      
            for(int j=len-1;j>=0;j--){
            System.out.print(inp[i][j]);
           }
        }
        
    }
  }

- Chintan September 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

C# implementation. Complexity O(n):

class ArrayIterator : IEnumerator {

        private readonly int[,] _array;

        private int _row, _col;
        private readonly int colSize;        
        
        public ArrayIterator(int[,] array) {
            _array = array;            
            colSize = _array.GetLength(1);            
            Reset();
        }

        public static int[] ToOneDimensional(int[,] array) {
            var rowSize = array.GetLength(0);
            var colSize = array.GetLength(1);
            var result = new int[rowSize * colSize];

            var it = new ArrayIterator(array);

            for (var i = 0; it.MoveNext(); i++) {
                result[i] = (int)it.Current;
            }

            return result;
        }

        // iterator O(n)
        public bool MoveNext() {
            // initial state O(1)
            if (_col == -1 && _row == -1) {
                _col++;
                _row++;
                return true;
            }
       
            // if even line move "right"
            if (_row % 2 == 0) {
                _col++;
                // if the end col is reached then move to the next row
                if (_col == colSize) {
                    _col--;
                    _row++;
                }
            }
            // if odd line move "left"
            else {
                _col--;
                // if the initial col is reached then move to the next row
                if (_col < 0) {
                    _col++;
                    _row++;
                }
            }
            
            return _row < _array.GetLength(0);
        }

        public void Reset() {
            _row = -1;
            _col = -1;
        }

        public object Current => _array[_row, _col];
    }

- ersha June 26, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public void matrixtoarray(int[][] arrays){
   for(int i = 0; i < arrays.length; i++){
      if(i % 2 == 0) 
         //addLeftToRight(arrays[i])
      else 
         //addRightToLeft(arrays[i])
   }
}

- Anonymous July 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

they ask 2 dimentional but they given ex is 3 dimentional array.

- dhivya July 15, 2016 | 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