Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: Phone Interview




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

Simple!!
try

Mat calc( Mat A , int n )
{
if( n == 1 )  return A;
Mat temp = calc ( A , n/2 );
temp = tempxtemp;  // matrix multiplication
if( n%2 == 1 ) temp = tempxA;  // matrix multiplication
return temp;
}

- Aakash01 July 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

With slight modification.

Mat calc( Mat A , int n )
{if( n == 2 ) return calc(A,A);
Mat temp = calc ( A , n/2 );
temp = tempxtemp; // matrix multiplication
if( n%2 == 1 )
temp = tempxA; // matrix multiplication
return temp;
}

- piyush290490 July 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Mat calc( Mat A , int n )
{if( n == 2 ) return calc(A,A);--- shud be A*A
Mat temp = calc ( A , n/2 );
temp = tempxtemp; // matrix multiplication
if( n%2 == 1 )
temp = tempxA; // matrix multiplication
return temp;
}

- Leo July 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why recursion when the following code can do the same.

K=A;

for ( i=1;i<n;i++)
{
k= K*A;
}

- hari@hbiz.in August 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Using n/2 is not necessary. It still takes k*T (T is the time cost to do a single matrix multiplication) on the kth recursion level. Thus the total running time is still O(T*n).

- shou3301 December 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Seems to be lg(n) * [matrix multiplication time] algorithm.

- Learner July 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Dynamic programming method can solve matrix chain multiplication of all sizes using minimum number of multiplication to watch and understand that you can see this wonderful video

youtu.be/OSkllTB2aW4

However for this special case of same square matrix divide and conquer is sufficient

- Anonymous July 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Let the function to calculate multiplications is calc(Mat A1, Mat A2)
calculate(Mat A, int n) {
   Mat temp = A
   if( n == 1){
      return A
   }
   for(int i = 1; i < n -1; i++){
      temp = calc(temp, A)
   }
   return temp
}

- bigbang4u2 July 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

matrix cal(matrix A,int n)
{
matrix result=matrix::indentity;
matrix tmp=A;
for(;n;n>>1)
{
if(n&1)
{
result=result*tmp;
}
tmp*=tmp;
}
return result;
}

- kurskk141 July 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

you can do this in log(n) .jst multiply till a^(n/2)*a^(n%2).

- medrocker August 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
int **fun_matrix(int **a,int **b)
{
int **c,i,j,k;
c=malloc(sizeof(*c)*3);
for(i=0;i<3;i++)
{

c[i]=malloc(sizeof(int)*3);
}


for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
{
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
}
}

return c;

}
void main()
{
int **a,**c;
int i,j,time,k;
int index;
int **result;
printf("enter the index of square matrix\n");
scanf("%d",&index);

a=malloc(sizeof(*a)*index);
for(i=0;i<index;i++)
{
a[i]=malloc(sizeof(int)*index);
}


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

for(i=0;i<index;i++)
{
for(j=0;j<index;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}

printf("How many time to multiply this matrix\n");
scanf("%d",&time);
result=a;
for(k=0;k<time;k++)
{
printf("this multiplication time=%d\n",k+1);
result=fun_matrix(result,a);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",result[i][j]);
}
printf("\n");
}
}

}

- ck saini July 19, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

This is dynamic programming where we need to find minimum steps to n.

- loveCoding July 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

How does this involve dynamic programming? It's a simple repeated-squares method, analogous to the problem of taking a power in log time.

- Anonymous July 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Breaking in sub problems can give better run time. To decide how to break, we need DP

- words&lyrics July 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think the whole purpose here is to minimize the calls to multiplication function... and to know that we need DP.
For e.g say we need to find A^11... you solution will call Multiplication 5 times whereas the optimized way to solve this would be to call mult 4 times

- loveCoding July 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

We do not need a dynamic programming here as the size of all the matrix are same for example if A is pXq and B is qXr then C=AB takes pqr multiplication (cost) Now lets say here all matrix are square matrix of size nXn and whatever way you multiply it will take the same cost so we can better use divide and conquer method which will take log(N) base 2 steps if number of square matrix to multiply is N
However the dynamic programming method can solve matrix chain of all sizes using minimum number of multiplication to watch and understand that you can see this wonderful video

youtu.be/OSkllTB2aW4

- Anonymous July 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Agree with the above comment, This is a simple problem similar to that of finding pow(x,n) in min number of calculations. since the matrices here are square matrices so using DP would give the same answer(i.e.) number of calculations no matter what. +1 to the above comment by annonymous.

- mr July 31, 2012 | 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