Amazon Interview Question for Software Engineer / Developers


Team: General
Country: United States
Interview Type: In-Person




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

We can do this by going left-to-right on A , calculating left of i , store in b, then from right-to-left calculating right of i multiply by b
Psedo code

M=1
for(i=0;i<n;i++)
{
B[i]=M;
M=M*A[i];
}

M=1
for(i=n-1;i>=0;i--)
{
B[i]=B[i]*M;
M=M*A[i];
}

- aziz November 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is the approach I'd expect on this question. Very solid O(1) lookup, O(n) extra space (assuming every value fits into O(1)) approach. I've posted another approach that is a bit slower but requires no extra space, assuming O(1) bits per integer.

- eugene.yarovoi November 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

You can always emulate division by doing a binary search and multiplication.

- eugene.yarovoi November 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i did not get you. can you explain?

- aakashv2.1 November 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@aakashv2.1 : I merely meant to say that this question is trivial if you can divide. But you can make a division operator by binary searching multipliers. When faced with a/b, let a/b = c and binary search for c, calculating b*c and comparing that to a.

- eugene.yarovoi November 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Really, since this requires no extra space (absent overflows), this may be a better approach than anything else in some cases. This takes O(d) time where d is the number of bits of precision an integer value can have.

- eugene.yarovoi November 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

O(d) time per lookup, I mean. O(n) multiplications initially to set it up.

- eugene.yarovoi November 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Why do we need division?

Isn't it easier to go B[3] = B[2] * A[2]
than

B[2] = B[3]/A[2]?

- Anonymous November 19, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use divide an conqueror.

Divide the array in 2 parts and when merging multiple the with the product of the array being merged in.

Eg.

[1 2 3 4]

[1 2] [3 4]

[1] [2] [3] [4]

This is how the B will be built where B has on values as [1 1 1 1]

[1*2] [1*1] [1*4] [1*3]

[2 1] [4 3]

[2*12 1*12 4*2 3*2]

[24 12 4 6]

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

Use divide an conqueror.

Divide the array in 2 parts and when merging multiple the with the product of the array being merged in.

Eg.

[1 2 3 4]

[1 2] [3 4]

[1] [2] [3] [4]

This is how the B will be built where B has on values as [1 1 1 1]

[1*2] [1*1] [1*4] [1*3]

[2 1] [4 3]

[2*12 1*12 4*2 3*2]

[24 12 4 6]

- abhishake2487 November 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

not making any sense .. can you run your algo for [1,2,3,4,5]

- choupsey November 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

func(array a, array b){
value temp = 1;
for(i=0; i<n-1; i++){
b[i] *= temp;
temp *= a[i];
}
temp = 1;
for(i=n-1; i>=0; i--){
b[i] *= temp;
temp *= a[i];
}
}

- Anonymous November 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think the code above is outputing the total product for each index

A little change will set it right

func(array a, array b){
value temp = 1;
for(i=0; i<n-1; i++){

// change these two lines..they are not correct
b[i] = temp;
temp = b[i]*a[i];
}
temp = 1;
for(i=n-1; i>=0; i--){
b[i] *= temp;
temp *= a[i];
}
}

- Anonymous November 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I am not able to follow the question. Can anyone explain it more clearly?

- Anonymous November 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey26729" class="run-this">#include<stdio.h>
#include<iostream>
using namespace std;

int main(){
int a[]={2,4,6,8};
int length=4;
int p[length];
int q[length];

//cumulative array by multiplying all but the element
p[0]=1;
p[1]=a[0];
for(int i=2; i<length;i++){
p[i]=p[i-1]*a[i-1];
}

//reverse cumulative by multiplying all but elements
q[length-1]=1;
q[length-2]=a[length-1];
for(int i=length-3; i>=0;i--){
q[i]=q[i+1]*a[i+1];
}

//display result
for (int i=0;i<length;i++){
cout<<p[i]*q[i]<<"\t";
}


}

//e.g A=[a,b,c,d];
//p=[1,a,a*b,a*b*c];
//q=[b*c*d,c*d,d,1];
//result equals multiplication of p & q;
//can we reduce space complexity??</pre><pre title="CodeMonkey26729" input="yes">;
</pre>

- Anonymous November 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

This can be done in one pass -

b[0] = a[0];
for ( int i = 1; i < n; i++ )
{
b[i] = b[i-1] * a[i-1];
}

- Anonymous November 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Tried using Recursion

public class dc_product {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] arr ={1,2,3,4,5,6};
		int[] brr=new int[arr.length];
		int j;
		for (j=0;j<arr.length;j++)
		{
			brr[j]=prod(arr,j,0);
			
			System.out.println(brr[j]);
		}
	}

	public static int prod(int[] a, int i, int m)
	{
		if (m==0 && i==0 )
		{
			if (a.length == 1)
				return a[i];
			return prod(a,i+1,1);
		}
		if (m==0 && i==a.length-1 )
			return prod(a,i-1,-1);
		if (i==0 || i==a.length-1)
			return a[i];
		if (m==0)
			return prod(a,i-1,-1)*prod(a,i+1,1);
		if (m==-1)
			return a[i]*prod(a,i-1,-1);
		if (m==1)
			return a[i]*prod(a,i+1,1);
		
		return (Integer) null;
	}
}

- guru November 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can someone please explain the problem with an example..

- AA December 11, 2011 | 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