Linkedin Interview Question


Country: United States
Interview Type: Phone Interview




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

import java.io.*;

class myCode
{
    public static void main (String[] args) throws java.lang.Exception
    {
        int n = 3;
        int m = 4;
        int 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 };
        
        //ist=>i_start
        //jst=>j_start
        int i=n-1,j=0,ist,jst,count=0;
        while(count<n*m){
            ist=i;jst=j;
            while(i<n && j<m){
                System.out.print(arr[i][j]+" ");
                count++;
                i++;j++;
            }
            System.out.println();
            i=(ist-1)>=0?ist-1:ist;
            j=(jst==0 && (ist-1)>=0)?0:jst+1;
        }
    }
}

- Sumit January 22, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

def printDiag(M, n, m):
    i, j = n - 1, 0
    while j < m:
        k, l = i, j
        res = ""
        while k < n and l < m:
            res += str(M[k][l])+ " "
            l += 1
            k += 1
        if i > 0:
            i -= 1
        else:
            j += 1
        print(res)
# Test
A = [[1, 2, 3, 4],
     [5, 6, 7, 8],
     [9,10,11,12]]

printDiag(A, 3, 5)

- Some Guy January 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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();

               }
           }
       }
               
    }
        
        
        
        
}

- sowmith.daram January 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

See Careercup question id=5695158902325248 for a similar question

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

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;
	}
}

- Atul Kumar Srivastava January 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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) ;

}

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

Its quite irksome when companies ask questions like these.. writing clumsy for loops prove nothing about ones knowledge of algorithms or coding. And in real life if you ever encounter a problem like this, one can always re-frame the problem and solve it in a much better way.

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

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);
  }
}

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

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

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

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;
}
}
}

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

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;
}
}
}

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

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;
            }
        }
    }

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

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();
		}

	}

}

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

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();
		}

	}

}

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

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++;
}
}
}

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

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++;
                }
            }

}

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

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++;
                }
            }

}

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

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++;
}
}
}

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

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();
        }
    }

- Praveen Gunasekaran January 11, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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();
}
}

- Praveen Gunasekaran January 11, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

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

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

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

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.

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

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.

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

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.

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

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();
			}
	}

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

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

- terminator June 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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();
        }




    }

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

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;
			}
		}
	}
}

- Alex August 09, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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++;
                }
            }

        }


    }

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

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++;
                }
            }

        }


    }

- va October 23, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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++;
                }
            }

        }


    }

- va October 23, 2017 | 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