Microsoft Interview Question for Applications Developers


Country: United States
Interview Type: Phone Interview




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

//The diagonals of a matrix are such that i = j. 
int matrix[row][col];
for (int i = 0; i < row; i++){
   //For matrices such that max({row,col}) = row
   if(i > col){
      break;
   }else{
      printf("%d \n", &matrx[i][i]);
   }
}

- Anonymous December 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

as all we know there exists two diagonals in a matrix....

for 1st diagonal

....
  for(i=0;i<n;i++)
  {
     for(j=0;j<n;j++)
          {
              if(i==j)
                   printf("%d ",matrix[i][j]);
          }
      }

for 2nd diagonal....

for(i=0;i<n;i++)
  {
     for(j=0;j<n;j++)
          {
              if((i+j==n)
                   printf("%d ",matrix[i][j]);
          }
      }

- rvndr December 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Though i highly doubt Msoft would ask this..
but why do u traverse the whole matrix Simply this would do

string diagonal1="";
string diagonal2="";
	for(int i=0;i<n;i++)
	{
	 cout<<a[i][i];
	cout<< a[n-1-i][i];
	}

- Sugarcane_farmer May 18, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

[(matrix[i][i], matrix[i][len(matrix) -i-1])  for i in range(len(matrix))]

- leosilva December 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Printing diagonals of a matrix is quite easy, but you need to understand the relation between matrix and diagonals. If you want perfect diagonals from matrix then your matrix must be square, I mean your matrix should be NxN. For example 2×2, 4×4 or 9×9 etc. Because in rectangular matrix (2×3 or 5×7) it is not possible to find perfect diagonals.

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

Now if you see above matrix carefully there are two diagonals in this one is 1, 6, 11, 16 and other is 4, 7, 10, 13.

I found this implementation, which is already done, you can check algo on below website link

somanyword.com/2013/12/print-diagonals-of-a-given-matrix/

- Maddy December 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String []args){
        System.out.println("Hello World");
        int [][] array = {{00,01,02,03,04},
                          {10,11,12,13,14},
                        {20,21,22,23,24},
                        {30,31,32,33,34},
                        {40,41,42,43,44}};
                        
        for(int i =0; i < array.length; i++){
            System.out.print(array[i][i]+" ");
        }
        System.out.println();

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

This will work only for square matrix....

- MegaTron February 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Please suggest .....

- MegaTron February 06, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

// test2.cpp : main project file.

#include "stdafx.h"
#include <iostream>

using namespace System;

#define M 3
#define N 3

bool testDiagonal(int n, int m, int input [][N])
{
    if (m != n) return false;

    for (int i=0; i<m; i++)
    {
        printf ("Diagonal: %d\n", input[i][i]);
    }
}



int main(array<System::String ^> ^args)
{
    int input[M][N] = {{1,2,3}, {4,5,6}, {7,8,9}};
    testDiagonal(M, N, input);

    return 0;
}

- J99 April 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

(According to the definition), Or m = min(m,n), instead of if (m != n) return false;

- J99 April 02, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

// test2.cpp : main project file.

#include "stdafx.h"
#include <iostream>

using namespace System;

#define M 3
#define N 3

bool testDiagonal(int n, int m, int input [][N])
{
    if (m != n) return false;

    for (int i=0; i<m; i++)
    {
        printf ("Diagonal: %d\n", input[i][i]);
    }
}



int main(array<System::String ^> ^args)
{
    int input[M][N] = {{1,2,3}, {4,5,6}, {7,8,9}};
    testDiagonal(M, N, input);

    return 0;
}

- J99 April 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class PrintMatrixDiagonally {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int[][] mat = { {1,2,3,4,5},
						{6,7,8,9,10},
						{11,12,13,14,15},
						{16,17,18,19,20}
					};
		printMatrixDiagonally(mat);

	}

	private static void printMatrixDiagonally(int[][] mat) {
		
		//Down side
		int col = 0;
		for(int row = 0; row < mat.length; row++){
			diagonally(mat, row, col);
			System.out.print("\n");
		}
		//Right side
		int row = mat.length - 1;
		for(col = 1; col < mat[row].length; col++){
			diagonally(mat, row, col);
			System.out.print("\n");
		}
		
	}
	//using recursion printing matrix diagonally
	private static void diagonally(int[][] mat, int row, int col) {
		
		//Bundary check
		if(row < 0 || col < 0 || row >= mat.length || col >= mat[0].length){
			return;
		}
		System.out.print(mat[row][col]+" ");
		diagonally(mat, --row, ++col);
		
	}

}

- Goutham April 18, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

Solution for main and incidental diagonals

public class Diagonals {

    public static void main(String[] args) {
        int[][] matrix = {{1,2,3}, {4,5,6}, {7,8,9}};
        new Diagonals().printDiagonals(matrix);
    }

    public void printDiagonals(int[][] matrix) {
        StringBuilder mainDiagonal = new StringBuilder();
        StringBuilder incidentalDiagonal = new StringBuilder();

        for (int i = 0; i < matrix.length; i++) {
            if (matrix[i].length != matrix.length) {
                System.out.print("Not square matrix");
                return;
            }

            mainDiagonal.append(matrix[i][i]);
            mainDiagonal.append(' ');

            incidentalDiagonal.append(matrix[i][matrix.length - i - 1]);
            incidentalDiagonal.append(' ');
        }

        System.out.println("main Diagonal: " + mainDiagonal);
        System.out.println("incidental Diagonal: " + incidentalDiagonal);
    }
}

- krylloff December 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Nice program. If I were you, I would check if the matrix is diagonal or not first and then declare the StringBuilders. Also rather checking it every time in the for loop, you can do it outside.

- Vijay December 15, 2013 | Flag


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