Amazon Interview Question for Applications Developers


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
5.this gives the answer

- ram rs September 16, 2013 | Flag Reply
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.

- Machinist September 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- gokayhuz September 16, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- sudiptosarka September 25, 2013 | Flag
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?

- Anonymous September 15, 2013 | Flag Reply
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.

- alex September 15, 2013 | Flag Reply
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

- Mohit September 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

couldn't get the question pls explain ...

- ram rs September 16, 2013 | Flag Reply
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?

- Pooja September 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I have the same questions.

- bigphatkdawg September 16, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- sudiptosarka September 25, 2013 | Flag
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++){
System.out.println("Adding elem "+ a[i][i]);
sumOfDiagnolElems += a[i][i];
}
if (order % 2 != 0){
for(i = 0,j = order -1 ; i < order && j >= 0; i++,j--){
if(i != j){
System.out.println("Adding elem "+ a[i][j]);
sumOfDiagnolElems += a[i][j];
}
}
}
System.out.println(" Sum of diagnol elems is "+ sumOfDiagnolElems);
}

}

- VB September 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- philippe.lhardy January 06, 2014 | Flag
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[0].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);
	}
}

- MacSan September 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- philippe.lhardy January 06, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Someone translate what the OP meant please.

- bigphatkdawg September 17, 2013 | Flag Reply
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[0].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);
}
}

- x September 17, 2013 | Flag Reply
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;
	}

- jeetendranarayan September 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

O(N) and clean code

- jeetendranarayan September 18, 2013 | Flag
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;
}

- Itanium September 18, 2013 | Flag Reply
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[0].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);

- Anonymous September 19, 2013 | Flag Reply
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.

- Anonymous September 20, 2013 | Flag Reply
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[][5] ={{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++) {
if (added == 0) {
added = 1;
sumOfDiagnolElems += a[i][j];
} else
added = 0;

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

- Anonymous September 24, 2013 | Flag Reply
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[3][3] = {{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;
}

- sudiptosarka September 25, 2013 | Flag Reply
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;
}

- Swaroop November 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int addoddorder()
{
int a[4][4]={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;
}

- Anonymous December 23, 2013 | Flag Reply
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 */

- are you guys dumb November 27, 2014 | 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