Microsoft Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: In-Person
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;
}
}
}
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;
}
}
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;
}
}
}
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));
}
}
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));
}
}
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("");
}
}
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();
}
}
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();
}
}
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]+" ");
}
}
}
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;
}
}
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++;
}
}
}
#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;
}
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();
}
}
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();
}
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++;
}
}
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();
}
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;
}
}
}
}
#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;
}
#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;
}
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] + " ");
}
}
}
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();
}
}
}
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];
}
- divm01986 July 15, 2016