## Amazon 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

This is very easy question....
We know that there are two diagonal of a matrix and both intersect at one(element) point if matrix is of size odd only.
Based on the above fact. Give below is working code of it.

``````int main(void)
{
int mat[SIZE][SIZE] = { ..........................  };
int i, j, dig1=0, dig2=0, sum=0;

//Sum of First diagonal.
for(i=0; i< SIZE; i++)
dig1+=mat[i][i];

//Sum of Second diagonal.
for(i=0; i< SIZE; i++)
dig2+=mat[i][SIZE-(i+1)];

if(SIZE % 2)
sum = dig1 + dig2 - mat[SIZE/2][SIZE/2];
else
sum = dig1 + dig2;

printf("\nSum of Digonal Elements of Matix without repeatation : %d\n", sum);

return 0;
}``````

Comment hidden because of low score. Click to expand.
-1
of 1 vote

y r u gvng jst the outline of prog..kindly post answer without errs..because it ll helpful for BEGINNERS..Any1 having BETTER solution??????

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

You are using middle element second time to calculate the sum of dig2. In question it is clearly mentioned that you can not use middle element twice. Not the Solution.

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

@Subbu2637
This is not just outline of program it is working program...... Try to run it, it will give result not error. Only things need to add is #include<stdio.h> & define SIZE with ur choice.
@DeeKaE
Yes i m using middle element but see the if condition and based on that i am calculation final sum after removing this element.

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

you dont need to loop till Size.

``````for(int i=0; i< SIZE/2; i++)
{
sum += array[i][i];
sum += array[i][SIZE-i-1];
sum += array[SIZE-i-1][i];
sum += array[SIZE-i-1][SIZE-i-1];
}
// if the array is of odd size the middle element is not covered
if(SIZE%2 == 1)
{
sum += array[SIZE/2][SIZE/2];
}``````

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

``````#define N 3
int main()
{
int matrix[N][N] = {.........}, sum=0,i;
input_matrix();
for(i=0;i<N;i++)
sum += matrix[i][i] + matrix[i][N-i];
if(N%2)
sum -= matrix[N/2][N/2];
printf("sum = %d", sum);
}``````

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

sum += matrix[i][i] + matrix[i][N-i];
is not correct it should be
sum += matrix[i][i] + matrix[i][N-i-1];

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

``````/* Similar, but handles the case where n == 1 a little more efficiently */
public static int
(IntegerMatrix im)
{
int total = 0;
int low = 0;
final int n = im.matrix.length;

for (; n/2 > low; low++) {
total += im.matrix[low][low] +  im.matrix[low][n-1-low];
total += im.matrix[n-1-low][low] + im.matrix[n-1-low][n-1-low];
}

if ((n & 0x1) == 1) total += im.matrix[low][low];

}``````

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

``````#define ROWS_COLS 5

int main()
{
int sum=0;
int a[ROWS_COLS][ROWS_COLS] = {{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15},{16,17,18,19,20},{21,22,23,24,25}};

for(int i=0;  i<ROWS_COLS ; i++)
{
if(i != (ROWS_COLS - i-1))
{
printf("Adding %d and %d to %d\n",a[i][i],a[i][ROWS_COLS-i-1],sum);
sum+=a[i][i]+a[i][ROWS_COLS-i-1];
}
else
{
sum+=a[i][i];
}
}
printf("Sum of diagnols = %d",sum);
getch();
return 0;
}``````

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

``````__int64 SumOfDiagonals(int* a, int n)
{
if (!a || !n) return -1;

__int64 sum = 0;
int i = 0, j = n - 1;
while (i < j)
{
sum += a[i*n + i] + a[i*n + j] + a[j*n + i] + a[j*n + j];
i++;
j--;
}
if (i == j) sum += 2 * a[i*n + i];
return sum;
}

int _tmain(int argc, _TCHAR* argv [])
{
int a [][3] = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
printf("Sum = %d\n", SumOfDiagonals((int*) a, 3));

int b [][4] = {
{ 1, 2, 3, 10 },
{ 4, 5, 6, 11 },
{ 7, 8, 9, 12 },
{ 13, 14, 15, 16 }
};
printf("Sum = %d\n", SumOfDiagonals((int*) b, 4));
return 0;
}``````

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

Every solution posted assumes, no_of_rows = no_of_cols, is it specified in the problem.
e.g arr[2][4] = { {1,2,3,4},
{5,6,7,8} }

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

``````if(row!=col)
return;
sumLeft=0;
sumRight=0;
for(i=0;i<row;i++)
{
if(row%2==0 && i==row/2)
sumLeft+=array[i][i];
else
{
sumLeft+=array[i][i];
sumRight+=array[i][n-i-1];
}
}
cout<<sumLeft<<"\t"<<sumRight;``````

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

//Declare rows columns and arrays
int r,c;
int arr[50][50];
int i, j;
//Get no of rows
printf("Enter rows and column: ");
scanf("%d %d", &r, &c);

//Get elements of array
for(i=0;i<r;i++){
for(j=0; j<c; j++){
scanf("%d",&arr[i][j]);
}
}
//initialise sum of both the diagonals to zero so that we can pile up the data in it
int sum1=0, sum2=0;

for(i=0; i<r; i++){ //loop for rows 0 to (no_of_rows -1 )
for(j=0; j<c; j++){ //loop for column 0 to (no_of_columns -1 )

if(j==i && j==c-i-1) {sum1+=arr[i][j];break;} // both first and second diagonal intersect..
//only sum of first diag be incremented
if(j==i) sum1+=arr[i][j]; //if column is equal to row i.e. first diagonal
if(j==(c-i-1)) sum2+=arr[i][j]; //if column is equal to no_of_column-1-row i.e. second diagonal
}
}
//print out the sum
printf(" %d %d", sum1, sum2);

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

import java.util.Scanner;
public class sum_Diagonals {
public static void main(String[] args){
int diag1 =0,diag2=0,sum=0;
Scanner in=new Scanner(System.in);
System.out.println("Enter the Size of Square Matrix");
int m=in.nextInt();
int a[][]=new int[m][m];
System.out.println("Enter the Elements of Matrix");
for(int i=0;i<m;i++)
for(int j=0;j<m;j++)
a[i][j]=in.nextInt();
System.out.println("The Elements of Matrix is");
for(int i=0;i<m;i++){
for(int j=0;j<m;j++)
System.out.print(a[i][j]+"\t");
System.out.println();
}
for(int i=0;i<m;i++)
diag1+=a[i][i];
System.out.println("sum of diagoal 1st \n"+diag1);
for(int i=0; i<m; i++)
diag2+=a[i][m-(i+1)];
System.out.println("Sum of second diagonal \n"+ diag2);
if(m%2==1)
sum=diag1+diag2;
else
sum=diag1+diag2-a[m/2][m/2];
System.out.println("Sum of Diagonals are \n"+sum);
}

}
//you can contact me on follwing email
//abdulmutalll786@gmail.com

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

``````public class test5 {

public static void main(String[] args) {

int a[][] = {{1,2,3,4,5},{1,2,3,4,5},{1,2,3,4,5},{1,2,3,4,5},{1,2,3,4,5}};
int sum = 0;
int row = a.length;
for(int i=0,j=row-1; i<row && j>=0; i++,j--)
{
if(i!=j)
{
sum = sum+a[i][i]+a[i][j];
}
else
{
sum = sum+a[i][i];
}
}

System.out.println("diagonal sum "+sum);
}

}``````

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

``````public class test5 {

public static void main(String[] args) {
// TODO Auto-generated method stub
int a[][] = {{1,2,3,4,5},{1,2,3,4,5},{1,2,3,4,5},{1,2,3,4,5},{1,2,3,4,5}};
int sum = 0;
int row = a.length;
for(int i=0,j=row-1; i<row && j>=0; i++,j--)
{
if(i!=j)
{
sum = sum+a[i][i]+a[i][j];
}
else
{
sum = sum+a[i][i];
}
}

System.out.println("diagonal sum "+sum);
}

}``````

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

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
int i, j,n, dig1=0, dig2=0, sum=0;

scanf("%d",&n);

int mat[n][n] ;

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&mat[i][j]);
}
}

for(i=0; i< n; i++)
{
dig1+=mat[i][i];
}

for(i=0; i< n; i++)
{
dig2+=mat[i][n-(i+1)];
}
if(n % 2)
{
sum = dig1 + dig2 - mat[n/2][n/2];
}
else
{
sum = dig1 + dig2;
}

printf("%d", sum);

return 0;
}

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

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
int i, j,n, dig1=0, dig2=0, sum=0;

scanf("%d",&n);

int mat[n][n] ;

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&mat[i][j]);
}
}

for(i=0; i< n; i++)
{
dig1+=mat[i][i];
}

for(i=0; i< n; i++)
{
dig2+=mat[i][n-(i+1)];
}
if(n % 2)
{
sum = dig1 + dig2 - mat[n/2][n/2];
}
else
{
sum = dig1 + dig2;
}

printf("%d", sum);

return 0;
}

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.

### 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.

### 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.

### 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.