Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

0. let the given array be called B.
1. keep an array A of size n fill in the first element.
2. keep the current size as s=1. keep a current index i (=0 to start with).
3. for each element x that is going to follow in B do a binary search in A starting from 0 ending at i to find out where x fits in A, say it fits in position k. Let us analyze k.
4.If the last element in A is smaller than x. then s=s+1 i=k+1. Go to step 3.
5. if k <i ,i=k, but s remains where it was earlier. Go to step 3.

At the end s will tell the size of the longest blah balh.

O(nlog(n)) is as follows: n for each element log(n) for the binary search we do for each element.

This is one of those algorithms that is not very intuitive, like the two stack solution for a iterative post order binary Tree traversal. Try to mug it.

- Dr.Sai December 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks for the explanation!

- SomeGuy December 13, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice... binary search makes it faster than other linear(backward) search of exact position and stack pop out versions... just little modification.. your algo still needs to store array A before discarding few elements.. lets say arrayList<int[]>.. store into index 0 only if previous content length is less than current.. (like what swathi mentioned in her algo.. )

- kn January 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Got O(n) solution with O(n) space complexity.
Start scanning the array from left to right... Have an empty stack..
1) if the current element is greater than the next element then push the element on to the stack..
2) else pop the stack untill the current element fit into the stack...
3) Make a note of the maximum sequence that is possible while you are moving between step1 to step2.

Example - array = {1 4 8 2 5 7 3 4 6}

push element 1 {1}
push element 4 (as it satisfies the increasing property) {1,4}
push element 8 {1,4,8}
try to push element 2 but since 2 is greater than 8 it breaks our property so pop untill the property satisfied. Also make a note of max seq possible so far {1,4,8}
now the stack content is {1,2}
push element 5 {1,2,5}
push element 7 {1,2,5,7}
now 3 breaks the rule so pop until the property satisfies and the max seq possible so far is {1,2,5,7}
now the stack contents are {1,2,3}
push element 4 {1,2,3,4}
push element 6 {1,2,3,4,6}

Hope i made my algo clear...

- swathi December 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

In your algorithm, the first element of the array is always part of the final result. What if the first element is not part of the longest subsequence?

- V M December 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I dont think the algorithm is correct.

It will fail for the following case.

{1,4,8,2,9,}

- Prakash December 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes Swathi algorithm will fail.

I will extend the Swathi Algorithm:
Start scanning the array from left to right... Have an empty array/vector..
1) if the current element is greater than the next element then push the element on to the array..
2) else Using binary Search, find the element in the array and replace the number just greater than equal to the current element.
3) Whatever remains in the array/vector will be the maximum subsequence

It is O(N log N) time and O(N) space complexity

- topcoder99 December 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

why it wil fail for {1,4,8,2,9} ?
the output of algorithm is as expect which is {1,4,8,9}... whatz wrong in my algo.. pls explain

- swathi December 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is a famous DP problem, and you can find the O(N^2) solution for it. However, I couldnt think of an O(NlogN) soln.

Also, to the one who posted, you mentioned constant space? I think its O(N) .. right ?

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

I don't think there's a constant time solution. From Wikipedia, the O(nLogN) solution:

L = 0
for i = 1, 2, ... n:
binary search for the largest positive j ≤ L
such that X[M[j]] < X[i] (or set j = 0 if no such value exists)
P[i] = M[j]
if j == L or X[i] < X[M[j+1]]:
M[j+1] = i
L = max(L, j+1)

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

Modified my original post .. I mena O(nlogn) instead of O(n).

And I was talking about contact space, not time ..

- P December 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Anonymous

Could you please explain the algo given in wikepedia?
I meant O(N log N) time with O(N) Space Complexity

- topcoder99 December 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

A good explanation - techpuzzl.wordpress . com/2011/12/10/longest-increasing-subsequence/

- V December 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

^^ I think the above will work.. ordering and position is not required. only subsequence required. but a better alternative would be this..

do a counting sort like iteration on the array. like so..

for i=0 to n
b[a[i]]++;

now b contains the count of each number.

next take two pointers on b. increment second if value in b > 0. set a when first value >0 found.. when second encouters 0 take difference of positions and change maximum..

which means 2 passes.. and O(n) solution..with O(n) space

please do tell if im wrong...
cheers

- Kshitij December 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I was referring to to quick sort method not the techpuzzle link :P

- Kshitij December 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

code for 2nd iteration in case u didnt get it

*p , *q = b;
while(*q != '\o')
{
    if(*q == 0)
     { *q++;
       }
else
      {
        p=q;
         while(*q != 0)
           { q++; }
        max = (p-q);

       }
}

- Kshitij December 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ok just read the question again!. 1234"5"6 the five is missing.. i was thinking consecutive numbers.!
and this place does not have a delete option. :/ lol..

- Kshitij December 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

check this link : anujdhamija.com/2011/10/27/longest-increasing-subsequence-problem/

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

do a quick sort which is o(nlogn). Then iterate the soreted array in 1 1 pass.
o(nlogn+n) still o(nlogn)

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

I think that would be incorrect because if you did a quicksort all of your numbers would monotonically increase :)

- Rio December 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

sort will scrabble ordering and positions of elements

- Anonymous December 09, 2011 | 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