Amazon Interview Question for Software Engineer / Developers






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

if i suppose that distance d1 is 1km, d2 is 2km, d3 is 3km......dn is nkm and has limited amount of petrol at every bunk say x,and x>1
in that case it would better to start at p1, once he reaches p1 again, he runs out of petrol and can start over again.

- suri May 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

f=0 // fuel
d=0 //distance
s=1 //best starting point

for i-> 1 to n
f+=fi
d+=di
if(f < d)
//reset s,f,d
s=i
f=d=0
endif
endfor

output s

O(n)

- Karthik May 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i think i solves in o(n^2) worst case, coz if i starts from 0 and when i<n and f<d then f and d needs to be constructed for i=1 all over again.

- suri May 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The cumulative array of petrol should be > cumulative array of distance for every element

distance array 1 3 2 1 3 5
petrol array 1 2 4 3 3 2

If we start at P1,
Cumulative array for distance = 1 4 6 7 10 15
Cumulative array for petrol = 1 3 7 10 13 15

If we start at P3,
Cumulative array for distance = 2 3 6 11 12 15
Cumulative array for petrol = 4 7 10 12 13 15

So P3 is our answer


Create a cumulative array starting from P1. Traverse the array and find the point (Px), where all elements to right of that

have property such that Pi > Di for all i > x AND
Pj > Dj for all j < x

Px+1 is your answer

otherwise not possible.
One example of not possible is

Distance array 2 8 5 4 1 6 2 2
Petrol array 3 5 7 3 5 3 2 2

- soron May 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

So you will have to create cumulative array starting at P1 then test, if fail then again create cumulative array at P2 and test...and so on.
Worst case time complexity O(n^2)

- Anonymous June 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No.. it would be O(n). you just need to traverse the cumulative arrays once.



Traverse the array and find the point (Px), where all elements to right of that

have property such that Pi > Di for all i > x AND
Pj > Dj for all j < x AND
Px < Dx

- soron June 11, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The solution is totally wrong! Look at the counter example you give:
Distance array 2 8 5 4 1 6 2 2
Petrol array 3 5 7 3 5 3 2 2

Starting from Petrol = 7 will complete the trip

- Anonymous October 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Only the counter example given by soron is wrong, but the solution is correct!

- aravind646 November 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Single cumulative array won't be enough I guess. When you construct cumulative array starting from P3 it will be different from when you construct cumulative array starting from P1. @Soron, can you please explain how can we determine Px from single cumulative array.

- coolpk December 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@ soron
awesome solution
hats off

- gulusworld1989 October 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A rather alternative approach would be construct differential array where diff[i] = p[i] - d[i]. Now try to find cumulative sum in the array starting from position j. In simple case any time sum < 0, we can't further travel from that point. We need to increment j and try to find if its feasible solution by fiinding cumulative sum of diff[j...n]+diff[0..j]. This is as good as rotating array at position j and finding the sum. Same effect can be achieved if diff array concatenated twice and finding the solution for it until 'n' elements are found.

petrol array 1 2 4 3 3 2
distance array 1 3 2 1 3 5

diff array 0 -1 2 2 0 -3

diff array concatentated 0 -1 2 2 0 -3 0 -1 2 2 0 -3

j = 0; // potential start position
sum = 0;
for(i = 0; i < 2n && (i -j) < n; ++i) { 
  sum += diff[i];
  if(sum < 0) { 
     j = i + 1;
     sum = 0;
  }    
}

return i == 2n ? -1 : j;

- coolpk December 02, 2010 | 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