## Amazon Interview Question for Applications Developers

• -2

Country: India
Interview Type: Written Test

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

1.consider left diagonal add the elements of left diagonal
2.consider right diagonal add the elements of right diagonal
3.add sum of right diagonal and left diagonal
4.subtract the mid element a[n/2][n/2] since it has to be used only once

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

Add the four corners of the matrix and recursively solve this for the inner matrices.

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

nice approach to a simple question, but why do you prefer a recursive solution when you can do this in a very simple way iteratively?

Also, the original question needs some clarification, as the question seems trivial as it is asked here

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

This approach won't work for a 4x4 matrix or any (even number)x(even number) matrix... Check out my implementation below.

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

what do you mean by middle element needs to be used only once?

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

The constraint is not clear. The middle element would be used only once by default, I guess.

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

We can add odd index numbers and that will be sum of the diagonal

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

couldn't get the question pls explain ...

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

Question is not quite clear, can there be an even matrix(4 x 4)? does it require sum of diagnol elements or all of the odd ordered elements?

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

I have the same questions.

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

Exactly the first thing that popped in my head... Made an implementation based on that below...

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

public class diagMatrixProblem {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int a[][] =new int[][]{{10, 25, 56,33,172},
{74, 18, 11,139,44},
{13, 16, 17,10,55},
{71, 54, 41,13,90},
{118,35, 44, 21, 74}};
int sumOfDiagnolElems = 0,i,j;
int order =5;
for(i = 0; i < order; i++){
sumOfDiagnolElems += a[i][i];
}
if (order % 2 != 0){
for(i = 0,j = order -1 ; i < order && j >= 0; i++,j--){
if(i != j){
sumOfDiagnolElems += a[i][j];
}
}
}
System.out.println(" Sum of diagnol elems is "+ sumOfDiagnolElems);
}

}

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

this is a C interview question, not a java one.

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

``````public class DiagonalMatrix {

public static void main(String[]args)
{
int arr[][] = { {1, 2, 3, 10, 30}, {4, 5, 6, 11, 31}, {7, 8, 9, 12, 32}, {21, 22, 23, 13, 33}, {40, 41, 42, 43, 44} };
int col_count = arr.length, row_count = arr.length, sum =0;

for(int i=0;i<row_count;i++)
for(int j=0;j<col_count;j++)
{
if(i==j)
{
sum+=arr[i][j];
if(arr[i][col_count-1-j]!=arr[i][j])
sum+=arr[i][col_count-1-j];
}
}

System.out.println(sum);
}
}``````

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

this is a C interview question, not a java one.

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

Someone translate what the OP meant please.

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

public class DiagonalMatrix {

public static void main(String[]args)
{
int arr[][] = { {1, 2, 3, 10, 30}, {4, 5, 6, 11, 31}, {7, 8, 9, 12, 32}, {21, 22, 23, 13, 33}, {40, 41, 42, 43, 44} };
int col_count = arr.length, row_count = arr.length, sum =0;

for(int i=0;i<row_count;i++)
for(int j=0;j<col_count;j++)
{
if(i==j)
{
sum+=arr[i][j];
if(arr[i][col_count-1-j]!=arr[i][j])
sum+=arr[i][col_count-1-j];
}
}

System.out.println(sum);
}
}

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

``````public static int getSum(int [][]a){
int sum = 0;
int len = a.length;
for(int i=0,j=0 ;i<len && j < len;i++,j++){
sum += a[i][j];

if(i != len-j-1)
sum += a[i][len-j-1];

}

return sum;
}``````

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

O(N) and clean code

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

``````int addDiagonals(int matrix[][])
{
int dim = findDimension(matrix);
int nIndex = dim-1;
int pIndex = 0;

int sum = 0;
int baseCursor = 0;
while(nIndex >= 0 && pIndex < dim)
{
if((matrix+baseCur+pIndex) == (matrix+baseCursor + nIndex))
sum+ = *(matrix+baseCursor+pIndex); // || In fact *(matrix+baseCursor+nIndex), both are same.
else
sum+ = *(matrix+baseCursor+pIndex) + *(matrix+baseCursor + nIndex);

pIndex++;
nIndex--;

baseCursor += dim;

}

return sum;
}``````

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

int a[][]={{4,6,7},{9,3,2},{1,4,8}};
int rc = a.length;
int cc = a.length;
int sum = 0;
for (int i = 0; i < rc; i++) {

for (int j = 0; j < cc; j++) {

if((i%2==0&&j%2==0)|| (i%2!=0&&j%2!=0))
{
System.out.println(a[i][j]);
sum = sum+a[i][j];
}
}
}

System.out.println(sum);

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

I think by odd-ordered, it means all elements whose indices sum to an odd value (it is my guess though which can be completely wrong). If that's the case, then we can find these elements and hash them to prevent duplicate values:

``````index=(I * row_size + j);
if(index%2 != 0)
{
val=*((int)*array+index);
if(hash[val]==-1)
{
hash[val]=1;
sum+=val;
}
}
delete [] hash;
return sum;``````

It is an O(n^2) time + O(n^2) space (worst case of all elements are unique) hence not a very efficient approach.

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

#include <stdio.h>

int main() {
// TODO Auto-generated method stub
int a[] ={{10, 25, 56,33,172},
{74, 18, 11,139,44},
{13, 16, 17,10,55},
{71, 54, 41,13,90},
{118,35, 44, 21, 74}};
int sumOfDiagnolElems = 0,i,j;
int order =5, added = 0;
for(i = 0; i < order; i++){
for(j = 0; j < order; j++) {
sumOfDiagnolElems += a[i][j];
} else

}
}
printf(" Sum of diagnol elems is %d \n", sumOfDiagnolElems);
return 0;
}

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

Doing exactly what the problem statement states, here's a C implementation. I haven't figured out how to do this in Java yet...

``````#include <stdio.h>

int main(void) {
int intArray = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int oddElementsSum = addOddElements(intArray, 3, 3);
printf("The sum of odd elements is: %d\n", oddElementsSum);
return 0;
}

int addOddElements(int *integerArray, int X, int Y) {
int size = X * Y;
int sum = 0;
int i = 0;
for(i = 0; i < size; i++) {
if(((i + 1)%2) != 0) {
sum = sum + integerArray[i];
}
}
return sum;
}``````

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

#include <stdio.h>

#define N 3

int main(void)
{
int a[N][N], i, j, sum = 0;

printf("Enter 3*3 matrix: \n");
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
scanf("%d", &a[i][j]);

for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
if ((i + j) % 2 == 0)
sum += a[i][j];

printf("\nODD ORDER SUM : %d", sum);
return 0;
}

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

{
int a={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
int *p=(int*)a;
int sum=0;
for(int i=0;i<16;i++)
{
sum=sum+((i%2)?0:(*(p+i)));
}
return sum;
}

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

if the array is an int array, let int * ptr = &array[X][Y];
then sum = sum + *ptr;
ptr = ptr + 2; /* Automatically adds every even element */

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.