Amazon Interview Question for SDE1s


Country: India
Interview Type: Written Test




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

We will have to solve this equation:


m-1 n-1 l-1
Σ Σ Σ 1
i=0 j=i+1 k=j+1

- Sunny January 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I did the algebra for these summations got this result:

((m-1)*((n*l) - ((l*m)/2) - (2 * l) - ((3/2)*n) - ((1/4)*m) - 3 - ((n**2)/2) + (((2*(m**2)) - m)/6)))

However, this does not produce the same results as the code, unfortunately. For instance, m=2; n=4; l=6 produces -8 instead of the desired 14

Maybe I made a mistake? I think the algebra is quite sound. I think the equation is wrong somehow...

- dantiston January 08, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 3 vote

we will have to solve this equation

m-1 n-1 l-1
Σ Σ Σ 1
i=0 j=i+1 k=j+1

=

m-1 n-1
Σ Σ ( l - j -2)
i=0 j=i+1



m-1
Σ (l-2)*(n-i-2) - (n^2/2 - n/2 - i^2/2 + i/2)
i=0


Solve the last equation and you will get the actual answer.

- Sunny January 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

According to Wolfram Alpha (wolframalpha.com), the sum you state is equal to 0.

http://www.wolframalpha.com/input/?i=sum+from+i%3D0+to+m-1+of+%28l-2%29*%28n-i-2%29+-+%28n%5E2%2F2+-+n%2F2+-+i%5E2%2F2+%2B+i%2F2%29

- dantiston January 07, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I did the algebra for these summations and got the same penultimate result, and then solved the final summation and got:

((m-1)*((n*l) - ((l*m)/2) - (2 * l) - ((3/2)*n) - ((1/4)*m) - 3 - ((n**2)/2) + (((2*(m**2)) - m)/6)))

However, this does not produce the same results as the code, unfortunately. For instance, m=2; n=4; l=6 produces -8 instead of the desired 14

- dantiston January 08, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 3 vote

sum=m*(l-2)*(n-1)

- Bharti Bhai January 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

summation over(n-1)(l-2)+(n-2)(l-3)+(n-3)(l-4)...(n-Z)(l-Z-1).... m times or till n-Z or l-Z-1 becomes negative

this has close form solution
summation (n-z)(l-z-1) = nl + z^2 +z - lz-nz-n
= nl + z^2 - (l+n-1)z -n
this summation can be calculated over 1-> Z
where Z = min(m,n,l-1)

- kkr.ashish January 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

the summation is

Zn(l-1) + Z*(Z+1)*( 2Z - 3l -3n +4)/6

Z=min(m,n,l-1)

test if any doubt

- kkr.ashish January 05, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If you try 4,4, 5 => Your method gives as a return 20 and it should return 10.

- nichoc January 08, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

sum will be incremented by (m-1)*(n-i-1)*(l-j-1) times.

- ashi January 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The answer is 1!+2!+---+ (l-2)!. I too could not find it directly. I had to debug and understand how the loop runs. It was completely dependent on the 'L' and not dependent on M and N. and by the way, since l-2 will be positive only when l>=3, the answer for l<=2 is 0.. for eg: if l = 6, then value is 1!+2!+3!+4! = 20..and for l=7 it will be 35.. and so on..

You can check it out..

- Srini.daruna January 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry i get the symbol wrongly.. i meant sum of integers not the factorial.. ∑(∑n) where n = 1 to l-2.. for the same example l = 6, sum of 1 to sum of 4..

- Srini.daruna January 05, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

check for n=0,n=1

for n=0;
answer is 0
for n=1
answer is 0

- kkr.ashish January 05, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, I have already mentioned that case in the answer.. as l-2 will be positive only if l >2, the value will be 0 for any value of l that <= 2

- Srini.daruna January 05, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

you answer cannot be independent of m and n
if you still dont get it use m=10,n=1,l=20

- kkr.ashish January 05, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

mrange = min(m, l-2)
Σ[(l-i-2)(l-i-1)/2] from 1 to mrange

sum = mrange*(mrange+1)*(2*mrange+1)/12 + mrange*(mrange+1)*(1-2*l)/4 + mrange*l*(l-1)/2;
The closed form can be further reduced.

- mar.mcjoan January 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Does not cover all the cases :(

- mar.mcjoan January 05, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

{
int ComputeFormula(const int& m, const int& n, const int& l) {
int mrange = std::min(m, l-2);
mrange = std::min(mrange, n-1);
int cstCorr = 0;
if(l > n) {
cstCorr = (l-n-1)*(l-n)/2;
}
return mrange*(mrange+1)*(2*mrange+1)/12 + mrange*(mrange+1)*(1-2*l)/4 + mrange*(l*(l-1)/2 - cstCorr );
}
}

This must work. Please challenge it.

- mar.mcjoan January 05, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

For (l >= n >= m):

int res = (int) (m * n * l - (m * n) / 2f * (1 + n) - m * (1 + m) / 2f * l +
				m * (1 + m) / 4f + m * (m + 1) * (2 * m + 1) / 12f);

- dromsik January 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

L * (L - 1) * (L - 2) / 6

- Anonymous January 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int sum=0;
now the answer will be independent of n and m.
for(k=2;k<l;k++){
sum+=∑(lower bound 1 and upper bound l-k) a;
}

- RK January 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

Look closely: sum = 0 => 0++ => always 0

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

In C++

// ConsoleApplication3.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream> 
#include <algorithm>
using namespace std;

double Sum(int k, int l, int m) 
{
double total = 0;
	int x= min(l-1, m-1);
	int y = min(x, k);
	total = (y) * m * x - m*(y*(y-1))/2   - (y) * ((x+1)*(x+2)) / 2 + y * (( y * (y +1))/2) - (  ((y+1)* y * (2*y +1))/6 - (y*(y+1))/2);
	return total;
}
int _tmain(int argc, _TCHAR* argv[])
{
	int k = 1; 
	int l = 2; 
	int m = 3; 
	int sum = 0; 

	for(int i=0; i< k; i++)
		for(int j= i+1; j< l; j++)
			for(int z=j+1; z < m; z++) 
				sum++;		
	cout << sum << endl;

	cout << Sum(k,l,m)<< endl;
	getchar();
	return 0;
}

Outputs
10
10

- nichoc January 07, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

why not just run the code he wrote.. why write more lines of code

- kkr.ashish January 07, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry the idea is to compare the results. And turn the loop into an expression to validate if they match.
To illustrate, I could come up with the Formula Eg.
m-1
Σ (l-2)*(n-i-2) - (n^2/2 - n/2 - i^2/2 + i/2)
i=0
But how do you validate that ?
I did not simplify but this is the idea...

- nichoc January 08, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

sum = Z*n*(l-1) + Z*(Z+1)*( 2*Z - 3*l -3*n +4)/6

Z=MIN( m, MIN(n,l-1) )

- kkr.ashish January 07, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Something off in the result:
Take for instance:
m, n, l
4, 4, 5

Correct Output = 10
Your output = 14

- nichoc January 08, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I tried to solve the equation.
And sum=m*m*m/6.0-(l-1)*m*m/2.0-(n*n/2.0-(l-1/2.0)*n+l/2.0-1/3.0)*m;

- chengyuanzhang831 January 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. First of all the condition should be: {n > 1 and l > n}

2. After solving the summation: (sum from i=0 to m-1 (sum from j = i+1 to n-1 (sum from k = j+1 to l-1 {1} ))), the result would be:

sol = sol1 - sol2 + sol3/12 - sol4, where

sol1 = (((l*n)-l)-n+1)*m;
sol2 = l*m*(m-1)/2;
sol3 = (9*m*(m-1)) + (m-1)*m*(2*m-1);
sol4 = n*(n-1)*m/2;

This could be further simplified..

If the answer is negative, then the sum would be 0

- AmalC January 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

l

- Anonymous January 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

should be 0 since for (int k = j + 1; k < l; k++) is never to execute that k always not ( k < 1 )

- William January 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Do you mean "( k < l )" not "( k < 1 )"? There's definitely a non-zero value.

- dantiston January 05, 2014 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

sum = (l-2) * (n-1) * m, provided 0<m<n<l
else sum = 0

- rushabhpasad January 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Dude: I think you didnt get the question the right way. The solution you have provided goes for these kind of loops:
Notice that the lengths of inner loops are not independent, these are depending on the iterating variables of the outer loops.

for (i = 0; i < m; i++)
    for (j = 1; j < n; j++)
    for (k = 2; k < l; k++)
    sum++;

- gaurav414u January 05, 2014 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

if (m <= 0 || n <= 1 || l <= 2) sum = 0;
else sum = (l-2) * (n-1) * m;

- uuuouou January 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

No.. The answer will never going to be dependent on n and m.. if condition is true but not the else statement in ur answer...

- Srini January 05, 2014 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

l

- Abhiram January 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 votes

Note the i+1 and j+1: this comes out to less than m * n * l

- dantiston January 05, 2014 | Flag
Comment hidden because of low score. Click to expand.


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