Linkedin Interview Question
Country: United States
Interview Type: Phone Interview
package nw.se;
/**
*
* @author sowmith
*/
public class NWSE {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int[][] inp = new int[][] {
{1,2,3,4},
{5,6,7,8},
{9,10,11,12}};
int temp;
for(int i = inp.length-1,j=0; i>=0 ; i--,j=0)
{
System.out.print(inp[i][j] + " ");
temp = i;
while(i+1 < inp.length && j+1 < inp[0].length)
{
System.out.print(inp[i+1][j+1] + " ");
i++;j++;
}
i = temp;
System.out.println();
if(i == 0)
{
for (int k = 1; k < inp[0].length; k++) {
temp = k;
System.out.print(inp[i][k] + " ");
while (i + 1 < inp.length && k + 1 < inp[0].length) {
System.out.print(inp[i + 1][k + 1] + " ");
i++;
k++;
}
k = temp;
i = 0;
System.out.println();
}
}
}
}
}
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class PrintMatrixNWSE {
private static int[][] arr;
private static int n;
private static int m;
private static Set<String> covered;
private static List<String> queue;
private static String sep = ":";
public static void main(String a[]) {
n = 4;
m = 4;
arr = new int[n][m];
arr[0] = new int[] { 1, 2, 3, 4 };
arr[1] = new int[] { 5, 6, 7, 8 };
arr[2] = new int[] { 9, 10, 11, 12 };
arr[3] = new int[] { 13, 14, 15, 16 };
covered = new HashSet<String>();
queue = new ArrayList<String>();
queue.add(n - 1 + sep + 0);
System.out.println(arr[n - 1][0]);
while (queue.size() != 0) {
queue = printNum(queue);
}
}
private static List<String> printNum(List<String> queue) {
List<String> aux = new ArrayList<String>();
for (String tmp : queue) {
String[] inds = tmp.split(sep);
int i = Integer.parseInt(inds[0]);
int j = Integer.parseInt(inds[1]);
if (j == 0 && i > 0) {
aux.add(i - 1 + sep + j);
System.out.print(arr[i - 1][j] + "\t");
}
if (j < m - 1 && !covered.contains(tmp)) {
aux.add(i + sep + (j + 1));
System.out.print(arr[i][j + 1] + "\t");
}
covered.add(tmp);
}
System.out.println();
return aux;
}
}
public void Print(int[,] array)
{
int minRow = 0, maxRow = array.GetLength(0) - 1,
minCol = 0, maxCol = array.GetLength(1) - 1,
startRow = maxRow, startCol = minCol,
currentRow = startRow, currentCol = startCol;
do
{
do
{
Console.Write(array[currentRow, currentCol] + " ");
currentRow++;
currentCol++;
}
while (currentRow <= maxRow && currentCol <= maxCol) ;
if (startRow != minRow)
startRow--;
else
startCol++;
currentRow = startRow;
currentCol = startCol;
Console.Write("\n");
}
while (startRow >= minRow && startCol <= maxCol) ;
}
public class MatrixZigZag {
public void print(int[][] matrix) {
for(int i = matrix.length - 1; i >=0; --i) {
printHelper(matrix, i, 0);
}
for(int j = 1; j < matrix[0].length; ++j) {
printHelper(matrix, 0, j);
}
}
private void printHelper(int[][] matrix, int row, int col) {
StringBuilder sb = new StringBuilder();
while(row < matrix.length && col < matrix[0].length) {
sb.append(matrix[row][col]).append(" ");
row = row + 1;
col = col + 1;
}
System.out.println(sb);
}
public static void main(String[] args) {
MatrixZigZag mz = new MatrixZigZag();
int[][] matrix1 = new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
int[][] matrix2 = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}};
mz.print(matrix1);
mz.print(matrix2);
}
}
Site ate the first comment, so trying again.
In Ruby:
def traverse(array)
array.each_with_index.flat_map{|r,i| r.each_with_index.map{|c,j| [c,i,j]}}
.sort_by{|c,i,j| [(j-i),j]}.group_by{|c,i,j| j-i}.map{|a,r| r.map{|c,i,j| c}.join(' ')}.join("\n")
end
puts traverse([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
9
5 10
1 6 11
2 7 12
3 8
4
public static void printmatrix() {
int[][] inp = new int[][] {
// {1, 2, 3, 4, 5, 7},
// {5, 6, 7, 8, 6, 4},
// {9, 10, 11, 12, 14, 11}
{1, 2, 3},
{5, 6, 7,},
{6, 10, 11,},
{7, 11, 16,},
{8, 13, 17,},
{9, 12, 18,}
// {9}
};
int n = inp.length; // row
int m = inp[0].length; // col
for (int i=0; i<n; ++i) {
System.out.println("----");
for (int j=0; j<min(i+1, m); ++j) {
System.out.print(inp[n-1-i+j][j]);
System.out.print(" ");
}
}
for (int i=1; i<m; ++i) {
System.out.println("----");
int j=1;
while ((i+j-1<m) && (j-1)<n) {
System.out.print(inp[j-1][i+j-1]);
System.out.print(" ");
++j;
}
}
}
public static void printmatrix() {
int[][] inp = new int[][] {
// {1, 2, 3, 4, 5, 7},
// {5, 6, 7, 8, 6, 4},
// {9, 10, 11, 12, 14, 11}
{1, 2, 3},
{5, 6, 7,},
{6, 10, 11,},
{7, 11, 16,},
{8, 13, 17,},
{9, 12, 18,}
// {9}
};
int n = inp.length; // row
int m = inp[0].length; // col
for (int i=0; i<n; ++i) {
System.out.println("----");
for (int j=0; j<min(i+1, m); ++j) {
System.out.print(inp[n-1-i+j][j]);
System.out.print(" ");
}
}
for (int i=1; i<m; ++i) {
System.out.println("----");
int j=1;
while ((i+j-1<m) && (j-1)<n) {
System.out.print(inp[j-1][i+j-1]);
System.out.print(" ");
++j;
}
}
}
public static void printmatrix() {
int[][] inp = new int[][] {
// {1, 2, 3, 4, 5, 7},
// {5, 6, 7, 8, 6, 4},
// {9, 10, 11, 12, 14, 11}
{1, 2, 3},
{5, 6, 7,},
{6, 10, 11,},
{7, 11, 16,},
{8, 13, 17,},
{9, 12, 18,}
// {9}
};
int n = inp.length; // row
int m = inp[0].length; // col
for (int i=0; i<n; ++i) {
System.out.println("----");
for (int j=0; j<min(i+1, m); ++j) {
System.out.print(inp[n-1-i+j][j]);
System.out.print(" ");
}
}
for (int i=1; i<m; ++i) {
System.out.println("----");
int j=1;
while ((i+j-1<m) && (j-1)<n) {
System.out.print(inp[j-1][i+j-1]);
System.out.print(" ");
++j;
}
}
}
package com.abhishek;
public class TestMatrixPrint {
public static void main(String[] args) {
int[][] arr = {
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 }
};
int N = arr.length, M = arr[0].length;
for (int i = N - 1; i >= 0; i--) {
int row = i;
int col = 0;
while (row < N && col < M) {
System.out.print(arr[row][col] + " ");
row++;
col++;
}
System.out.println();
}
for (int j = 1; j < M; j++) {
int col = j;
int row = 0;
while (col < M && row < N) {
System.out.print(arr[row][col] + " ");
col++;
row++;
}
System.out.println();
}
}
}
package com.abhishek;
public class TestMatrixPrint {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] arr = {
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 }
};
int N = arr.length, M = arr[0].length;
for (int i = N - 1; i >= 0; i--) {
int row = i;
int col = 0;
while (row < N && col < M) {
System.out.print(arr[row][col] + " ");
row++;
col++;
}
System.out.println();
}
for (int j = 1; j < M; j++) {
int col = j;
int row = 0;
while (col < M && row < N) {
System.out.print(arr[row][col] + " ");
col++;
row++;
}
System.out.println();
}
}
}
static void printDiag(int [,] mtx)
{ int x = mtx.GetLength(0)-1;
int y = 0;
while(x<= mtx.GetLength(0)-1 && y<= mtx.GetLength(1)-1){
int ydim = y;
int xdim = x;
while(xdim <= mtx.GetLength(0)-1 && ydim <= mtx.GetLength(1) - 1)
{
Console.Write(mtx[xdim, ydim]);
Console.Write(" ");
xdim++;
ydim++;
}
Console.WriteLine();
if (x > 0)
{
x--;
}
if(ydim > mtx.GetLength(0) - 1)
{
y++;
}
}
}
static void printDiag(int [,] mtx)
{ int x = mtx.GetLength(0)-1;
int y = 0;
while(x<= mtx.GetLength(0)-1 && y<= mtx.GetLength(1)-1){
int ydim = y;
int xdim = x;
while(xdim <= mtx.GetLength(0)-1 && ydim <= mtx.GetLength(1) - 1)
{
Console.Write(mtx[xdim, ydim]);
Console.Write(" ");
xdim++;
ydim++;
}
Console.WriteLine();
if (x > 0)
{
x--;
}
if(ydim > mtx.GetLength(0) - 1)
{
y++;
}
}
}
static void printDiag(int [,] mtx)
{ int x = mtx.GetLength(0)-1;
int y = 0;
while(x<= mtx.GetLength(0)-1 && y<= mtx.GetLength(1)-1){
int ydim = y;
int xdim = x;
while(xdim <= mtx.GetLength(0)-1 && ydim <= mtx.GetLength(1) - 1)
{
Console.Write(mtx[xdim, ydim]);
Console.Write(" ");
xdim++;
ydim++;
}
Console.WriteLine();
if (x > 0)
{
x--;
}
if(ydim > mtx.GetLength(0) - 1)
{
y++;
}
}
}
static void printDiag(int [,] mtx)
{ int x = mtx.GetLength(0)-1;
int y = 0;
while(x<= mtx.GetLength(0)-1 && y<= mtx.GetLength(1)-1){
int ydim = y;
int xdim = x;
while(xdim <= mtx.GetLength(0)-1 && ydim <= mtx.GetLength(1) - 1)
{
Console.Write(mtx[xdim, ydim]);
Console.Write(" ");
xdim++;
ydim++;
}
Console.WriteLine();
if (x > 0)
{
x--;
}
if(ydim > mtx.GetLength(0) - 1)
{
y++;
}
}
}
public static void PrintMatrixDiagonallyTopDown(int[][] matrix){
int row , column;
int rowCount = matrix.length;
int colCount = matrix[0].length;
for(int k = rowCount-1;k>=0;k--) {
for(row =k, column=0;row<rowCount && column< colCount;row++,column++){
System.out.print(matrix[row][column]+" ");
}
System.out.println();
}
for(int k =1;k<colCount;k++) {
for(row=0, column=k;row<rowCount && column<colCount;row++,column++){
System.out.print(matrix[row][column]+" ");
}
System.out.println();
}
}
public static void PrintMatrixDiagonallyTopDown(int[][] matrix){
int row , column;
int rowCount = matrix.length;
int colCount = matrix[0].length;
for(int k = rowCount-1;k>=0;k--) {
for(row =k, column=0;row<rowCount && column< colCount;row++,column++){
System.out.print(matrix[row][column]+" ");
}
System.out.println();
}
for(int k =1;k<colCount;k++) {
for(row=0, column=k;row<rowCount && column<colCount;row++,column++){
System.out.print(matrix[row][column]+" ");
}
System.out.println();
}
}
start pointer p to go from bottom-left to top-left and then top-left to top-right
for each p (x,y), start with i=x, j=y and iterate i += 1, j+= 1 while printing until
you hit boundaries, at which point you print new line.
def print_matrix_diagonal(M):
if M:
n = len(M)
m = len(M[0])
x = n-1
y = 0
while x > 0 :
i = x
j = 0
print_diagonal(M, i, j, n, m)
x -= 1
while y < m:
i = 0
j = y
print_diagonal(M, i, j, n, m)
y += 1
def print_diagonal(M, i, j, n, m):
while i < n and j < m:
print M[i][j],
i += 1
j += 1
print
===========
>>> print_matrix_diagonal(M)
9
5 10
1 6 11
2 7 12
3 8
4
start pointer p to go from bottom-left to top-left and then top-left to top-right
for each p (x,y), start with i=x, j=y and iterate i += 1, j+= 1 while printing until
you hit boundaries, at which point you print new line.
def print_matrix_diagonal(M):
if M:
n = len(M)
m = len(M[0])
x = n-1
y = 0
while x > 0 :
i = x
j = 0
print_diagonal(M, i, j, n, m)
x -= 1
while y < m:
i = 0
j = y
print_diagonal(M, i, j, n, m)
y += 1
def print_diagonal(M, i, j, n, m):
while i < n and j < m:
print M[i][j],
i += 1
j += 1
print
>>> print_matrix_diagonal(M)
9
5 10
1 6 11
2 7 12
3 8
4
public void getDiagonalString() {
int[][] inp = new int[][] {
{1 ,2 ,3 ,4 ,5,100},
{6 ,7 ,8 ,9 ,10,200},
{11,12,13,14,15,300},
{16,17,18,19,20,400},
{21,22,23,24,25,500},
{26,27,28,29,30,600}};
System.out.println(inp[inp.length-1][0]);
int count;
int tempi ;
for( int i= inp.length-2; i>=0;i--){
count= inp.length-i;
tempi= i;
for( int j=0;j<=count;j++){
if(i>inp.length-1){
i =tempi;
break;
}
System.out.print(inp[i++][j] + " ");
}
i = tempi;
System.out.println();
}
int tempj;
for(int j=1;j<=inp[0].length-1;j++){
count= inp.length-j;
tempj =j;
for(int i = 0 ; i <=count;i++){
System.out.print(inp[i][j]+ " ");
if(j==inp[0].length-1)
break;
j++;
}
j = tempj;
System.out.println();
}
}
public class MatrixPrintNWSE {
private static void printDiagonal(int[][] mat, int row, int col) {
int rmax = mat.length - 1;
int cmax = mat[0].length - 1;
int crow = row;
int ccol = col;
while (crow <= rmax && ccol <= cmax) {
System.out.print(mat[crow++][ccol++] + " ");
}
}
public static void print(int[][] mat) {
int rmax = mat.length - 1;
int cmax = mat[0].length - 1;
int row = rmax;
int col = 0;
while (col <= cmax) {
printDiagonal(mat, row, col);
System.out.println();
if (--row <= 0) {
row = 0;
col++;
}
}
}
public static void main(String[] args) {
int[][] mat = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
MatrixPrintNWSE.print(mat);
}
}
Output :
9
5 10
2 7 12
3 8
4
public static void main(String args[]){
int [][]arr = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
int rStart=arr.length-1;
int cStart=0;
int count=arr.length*arr[0].length;
for(int i=0;i<=count;i++){
int j=0;
while((rStart+j)<arr.length && (cStart+j)<arr[0].length){
System.out.print(arr[rStart+j][cStart+j]+" ");
j++;
}
if(--rStart<0){
rStart=0;
cStart++;
}
System.out.println();
}
}
void PrintDiagonals(vector<vector<int>> const &matrix)
{
if (!matrix.empty() &&
!matrix[0].empty())
{
int m = matrix.size();
int n = matrix[0].size();
int start_r = m - 1;
int start_c = 0;
while (start_c < n) {
int r = start_r;
int c = start_c;
while (r < m &&
c < n)
{
cout << matrix[r][c] << ", ";
++r;
++c;
}
cout << "\n";
if (start_r > 0) {
--start_r;
} else {
++start_c;
}
}
}
}
void printMatrix(int[][] matrix, int i, int j) {
int row = i-1;
int col = 0;
int runningRowCount = row;
int runningColCount = 0;
while(row >= 0 && col < j) {
System.out.print(" " + matrix[row][col]);
// this is the exit condition
if(row == 0 && col == j-1) {
return;
}
if(row == i-1) {
if (runningRowCount != 0) {
runningRowCount--;
runningColCount=0;
} else {
runningColCount++;
}
row = runningRowCount;
col = runningColCount;
System.out.println(" ");
} else {
row++;
if(col == j-1) {
runningRowCount=0;
row = runningRowCount;
runningColCount++;
col = runningColCount;
System.out.println(" ");
} else {
col++;
}
}
}
}
static void printMatrix(int[][] matrix, int i, int j) {
int row = i-1;
int col = 0;
int runningRowCount = row;
int runningColCount = 0;
while(row >= 0 && col < j) {
System.out.print(" " + matrix[row][col]);
// this is the exit condition
if(row == 0 && col == j-1) {
return;
}
if(row == i-1) {
if (runningRowCount != 0) {
runningRowCount--;
runningColCount=0;
} else {
runningColCount++;
}
row = runningRowCount;
col = runningColCount;
System.out.println(" ");
} else {
row++;
if(col == j-1) {
runningRowCount=0;
row = runningRowCount;
runningColCount++;
col = runningColCount;
System.out.println(" ");
} else {
col++;
}
}
}
}
static void printMatrix(int[][] matrix, int i, int j) {
int row = i-1;
int col = 0;
int runningRowCount = row;
int runningColCount = 0;
while(row >= 0 && col < j) {
System.out.print(" " + matrix[row][col]);
// this is the exit condition
if(row == 0 && col == j-1) {
return;
}
if(row == i-1) {
if (runningRowCount != 0) {
runningRowCount--;
runningColCount=0;
} else {
runningColCount++;
}
row = runningRowCount;
col = runningColCount;
System.out.println(" ");
} else {
row++;
if(col == j-1) {
runningRowCount=0;
row = runningRowCount;
runningColCount++;
col = runningColCount;
System.out.println(" ");
} else {
col++;
}
}
}
}
- Sumit January 22, 2017