Amazon Interview Question for Software Engineer / Developers






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

1. Create a stack of n elements. Push A[0] onto stack.
2. Maintain two variables Min, Max. Initially Min = Max = A[0].
3. for i: 1 to n-1
if A[i] <= Min
Push A[i] onto stack
Min = A[i]

else if A[i] > Max
'i' will be the closest greatest element to the right for all
the elements in the stack
Pop all the elements in the stack and update the result array

else (if A[i] > Min & A[i] <= Max)
pop all elements < A[i]
'i' will be the closest greatest element to the right for all
the popped elements
Update Min = topmost element on the stack
Space: O(n)
Time : O(n)

- Guest123 August 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This works.

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

I don't think your algo is O(n).

Consider this array

100 8 90 <-> 80 9 8 90 <-> 80 9 8 90 <-> 80 9 8 90 <-> 80 9 8 90 <-> 80 9 101

where 90 <-> means 90, 89 , 88 upto 80 etc.

Now if you run your algo whenever you hit a 8 you will backtrack to the begining and add everything to the stack again expect the 8. so worst case will be n^2 right.

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

I don't think your algo is O(n).

Consider this array

100 8 90 <-> 80 9 8 90 <-> 80 9 8 90 <-> 80 9 8 90 <-> 80 9 8 90 <-> 80 9 101

where 90 <-> means 90, 89 , 88 upto 80 etc.

Now if you run your algo whenever you hit a 8 you will backtrack to the begining and add everything to the stack again expect the 8. so worst case will be n^2 right.

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

One you pop some elements, you never revisit them.

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

Not really... Consider this

100 8 90 <-> 80

and what happens when you encounter a 9.

Now if this is a stack you will have to pop everything from 80 <-> 90 ... remove 8 and update its nearest great as 9 and push 90 <-> 80 again.

so al the segments labelled 90 <-> 80 get pushed and popped over and over again.

Even if you don't done use a stack you will have to backtrack one by one untill your reach an 8

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

Actually it works.
For the array: 100 8 90 <-> 80 9 8 90 <-> 80 9 8 90 <-> 80 9 8 90 <-> 80 9 8 90 <-> 80 9 101
Stack at each step
100 : 100
8 : 100 8
90 : 100 90 //pop 8 & push 90, it's CGE(closest greatest elm) is 90
89<->80 9 8 90 <-> 80 9 8 90 <-> 80 9 8 90 <-> 80 9 8 90 <-> 80 9 all goes on stack(Max:100, min:8)
once you encounter 101, all the elements are popped and assigned 101 as CGE
push 101 on stack continue
Therefore, O(n).

- Guest123 August 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Also, we don't have to push the same element twice. We compare the current element with Min & Max before push.

- Guest123 August 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorry...messed up with the explanation
90 : 100 90 //pop 8 & push 90, it's CGE(closest greatest elm) is 90
89<->80 9 8 goes on stack
now if we encounter 90, all the elements till 89 are popped from the stack & 90 is pushed
so stack will be: 100 90 90
the process continues

- Guest123 August 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This isn't really O(n), see because the statement

pop all elements < A[i]

this is really a loop and would require popping all the elements currently in the stack and pushing back those that are greater than A[i]. Unless you can prove this loop/popping would run for a constant number of elements, I am not sure how the algo you suggested is O(n)?

- anonymous August 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The elements in the stack will always be stored in descending order. So the statement pop all elements < A[i] is same as pop elements till element >= A[i]. And push A[i].
There by maintaining descending order. So, each element will be pushed once and popped once. Hence O(n).

- Guest123 August 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes this work.

Observe that the following invariant holds: At anytime, if the stack is non-empty, then the elements are in decreasing order (or non-increasing) from the bottom to the top.

Now on getting a new element, the algorithm either pushes a smaller element on top, or pops off some elements till the invariant holds with the new element on the stack and for these popped of elements, the closest greater element is the new element we are trying to add.

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

yup agreed works ! good job!

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

I doubt that this can run in O(n) time. It should be O(nlog n)time.
Although the above code can be improved by traversing list from right. Below i have given code to do this. This has running time O(nlog n) and space complexity(n).

// Maintain a stack which may go up to max n. Say stack[0..n-1]

function main(){
    O[0..n-1] = {NULL,NULL,...,NULL};
    stack[0..n];
    _stack_i = -1; // Stack Index
    PUSH(stack,-1); // Push -1(i.e. No match) to stack
    O[n-1] = TOP(stack); // Last element of list has no match
    for(i = n-1 ; i >= 0 ; i--){
        if(A[i] >= A[i+1])// If array elements are in decreasing 
            O[i] = TOP(stack); //order from right to left, then no change in match
        else{
            O[i] = FETCH_AND_POP_GREATER_KEY(A[i], stack); // fetch match > key(i.e. A[i]) and make this as new match for further processing
            PUSH(A[i+1], stack);
        }
    }
    PRINT O[0..n-1];
}

function TOP(stack){
    if(_stack_i < 0)
        return NULL;
    else
        return stack[_stack_i];
}

function PUSH(elem, stack){
    stack[++_stack_i] = elem;
}

### Below function finds the element, from stack, which is higher than 'key'and pops all elements <= key.

function FETCH_AND_POP_GREATER_KEY(key, stack){
    index = mod_binary_search(key, 1, _stack_i, stack);
    if(index > 0)
        _stack_i = index;
    else
        _stack_i = 0;
    return stack[_stack_i];
}

### Below function finds index of element which is just next higher to the search 'key' element. NOTE : stack is already sorted in descending order. (Avg running time O(log n))

function mod_binary_search(key, start, end, array){
    if(start > end)
        return -1;
    if(start == end){
        if(array[start] > key)
            return start;
        else
            return -1;
    }
    index = (end-start)/2;
    if(array[index] < key)
        return mod_binary_search(key,index+1,end,array);
    if(array[index] >= key)
    {
        if(index > start && array[index-1] < key){
            return index; 
        }
        return mod_binary_search(key, start, index-1, array);
    }
}

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

@Guest123: Nice solution..

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

your solution is wrong because consider a case like

7 6 5 4 10 9 8, in that case you would have considered the popping out for all elements.
And when did popping out all the elements from the stack became O(1)

- iPranjul August 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

There are two kind of analysis: worst case analysis, amortized analysis.
The worst case for this problem is definitely O(n2), but amortized cost is O(n), which is good enough in general.

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

@Ipranjul and @Anonymous. Wtf are you guys talking about?

The solution given works for your example in O(n) time. No quadratic worst case etc.

It is O(n) worst case. PERIOD.

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

It's sad that I actually have to read your solution to understand what the question is asking about. Sounds like it should be "closest greater element" instead of "closest greatest element".

Anyways, sounds like you don't really need the max variable here. You can just pop until the top element is greater than A[i].

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

Extension to that question , find the next greatest element of particular number in the sequence.

- Guest August 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

For the first problem some of the ideas which i got are :
1)Run two for loops and check for all cases which has a value more than current value . Time complexity 0(n2)
2)Finding all the increasing sequences
3)Patience sorting with multiple buckets also help but not memory efficient.
4)Form a Binary Tree of the nodes , in the given order . The given Node contains infomation of index value , for every given node find the succesor which has index greater than current index , still worst case 0(n2)

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

O(nlogn) is possible.

Say there was a datastructure which supported this:

- Insert (key, value) - Insert key with value value.
- GetLeastValue(key1, key2) - Of all keys k such that key1 < k < key2 , return the value which is the least.

With trees, we can do both operations in O(log n) time.

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

This works, and is a more general solution I guess.

For instance if we wanted the furthest rather than the closest: It does not seem obvious to modify the stack based O(n) solution to modify for that.

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

Total complexity: )(nlogn)

create a binary search tree with the given values in the array which will take O(nlogn) time now for each element in the array find the element in the binary search tree and find the immediate successor of that node.... this will take O(nlogn)

thus total of : O(nlog n) + 0(nlogn) = 0(nlogn)

- raja August 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

but if the binary tree turns into a linked list then the complexity won't be O(nlgn)

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

But if the tree is balanced there is no linked list.

- Anonymous August 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I think this would be the best way to approach this problem. The stack solution seems over complicated.

- Varun September 02, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Multiple problems with this solution. Consider something like: 10 11 12 9 8 6 7 1000.
If the stack is (10), when we see 11, we pop 10, update it, and then don't do anything to the stack. The stack is now empty and 11 will never get updated.

If you push 11 onto the stack after popping, you still have a problem. Let's say I've processed 10 11 12 9 8 6 so far. The stack is now (12 9 8 6). When I see 7, I'll
pop off 6 and set min=8. So, what, now I see 1000. I pop everything and I never updated 6 and the previously updated elements will never be updated, so only 12 9 8
are updated. Even if I push 7 onto the stack, then only 12 9 8 7 get the correct value.

The correct solution for this one is 7 for all indices except the last index.

Algorithm is O(n), but the solution is incorrect.

This is solvable in O(n) time and space though using the RMQ algorithm, but there should be a simpler way.

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

@raja: What the fucking solution is dis?? That too you have posted after seeing Guest123 solution....... Along with u some more fools posting for doubts on the f'ing solution....

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

Can somebody please explain the problem a little bit better? I got confused, what does "closest greatest" mean?

Say for an array: 1 100 1000, for A[0], 100 would be the closest element that is greater than itself, but 1000 would be the greatest element, I guess the question was referring to the first scenario?

It'd be great if you can post some examples. Thanks!

- airfang613 August 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{{public static void next(int[] a,int x){
int i=0,j=a.length;
int next=(int)Math.pow(2,31);
int inext = 0;
int count=0;
int diff=(int) Math.pow(2,31);
for(i=0;i<j;i++){
if(a[i]>x){
next=a[i];
if((next-x)<diff){
inext=next;
diff=next-x;
next=0;

}
}else{
count++;
}

}if(count<i)
System.out.println(inext);
else{
System.out.println("No larger elements");
}
}
}}

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

This works in O(n)

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

I think the problem is to find the next closest greater element for every element in array, not for a given x.
For a given x, naive approach itself takes O(n) time.

- Sriram S August 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Could anyone please explain the pproblem with a simple example....From the question what I understood was, In an array of elements, find the closest greatest element,
Eg: A[] ={5, 8, 20, 1, 4}
Solution:(8, 20,null, 4, 5}
Closest to A[0] = 8
Closest to A[1] = 20
Closest to A[2] = none
Closest to A[3] = 4
Closest to A[4] = 5

If this is the problem, we could do the following steps:
1. Sort the array A[] into B[], O(nlogn)
2. In one pass of B make an entry into Hashtable<B[i], B[i+1]>
3.The successor of any index in A[] can be in obtained using the Hash<A[i]>

Please correct me if I am wrong

- San August 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

No, i think your understanding is not correct.
The solution to your example = {8, 20, null, 4, null}
For every element - A[i], we need to find an element x such that x is to the right of A[i] and x>A[i]. The first such x needs to be computed for every A[i].

- Sriram August 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Then I think problem should be modified to closest greater element to its right.

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

std::vector<int> input; //contains the input array
	std::map<int,int> outputmap; //contains the output as a map
	std::stack<int> current; // a stack to maintain pairs that have not been found yet
	
	for ( size_t i = 0; i < input.size() ; ++i )
	{
		while( current.size() > 0 && current.top() < input[i] )
		{
			int topElement = current.top();
			outputmap[topElement] = input[i];
			current.pop();
		}
		current.push( input[i] );
	}

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

Analysis:
1. The naïve solution yield O(N^2) solution as for each element we findMax() on the right subarray.
2. We can see that if a[i+1] < a[i], it should be somehow remember but findMax( ) redundancy compare many times.
3. Instead of looping from left to right we can use a stack-based approach and run backward. Having the stack keeping a[k] if a[k] is greater than the array to its left down to a[i] (i is our iterator). Hence, the closest greatest element to the right of a[i] is stack.top( ).
4. The improved solution is O(N)
Pseudo Code:

int array[N]
int answer[N]
find_closest_greatest( ){
    Stack<int> stack = new Stack<int>();
    for(int i=N-1; i>-1; i--){
         if(stack.empty( )  ||  stack.top < a[i]) 
            stack.push(a[i]);
         else; //do nothing
         answer[i] = stack.top( );
    }//endfor
}//end function

- Saimok August 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This code does not work! say array = [5,8,20,1,4], then answer = [20,20,20,4,4]
Moreover worst case this will take O(nlog n) time @ O(n) space complexity. It cannot be better than this.

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

Isn't the question ambiguous, which takes priority, the "closeness" of the index, or the "greatness" of the larger element?

Consider input like: [1,2,3,4], should the output be: [2,3,4,-], or [4,4,4,-]

- Good Mann August 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int result[1024];
int totallen;

void init() {
    for(int i = 0; i < 1024; i++)
        result[i] = -1024;
}

void solve(int a[], int len){
    init();
    stack<int> stk;
    stk.push(0);
    int min = a[0];
    int max = a[0];
    for(int i = 1; i < len; i++){
        if(a[i] <= min) {
            stk.push(i);
            min = a[i];
        }else if(a[i] > max) {
            while(!stk.empty()){
                int t = stk.top();
                stk.pop();
                result[t] = a[i];
            }
            max = a[i];
            stk.push(i);
        }
        else if(a[i] > min && a[i] <= max) {
            while(!stk.empty()) {
                int t = stk.top();
                if(a[t] < a[i])
                {
                    stk.pop();
                    result[t] = a[i];
                }
                else
                    break;
            }
            stk.push(i);
            min = a[i];
        }
    }
}

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

#include "stdafx.h"
#include "iostream"
using namespace std;

int FindLargest(int *a, int start, int n)
{
	if(start < n-1)
	{
		int val = a[start];
		int i = start+1;
		for(i = start+1; i < n; i++)
		{
			if(a[i] <= val)
			{
				continue;
			}
			else
			{
				break;
			}
			
		}
		if(i < n)
		{
			cout << val << a[i] << endl;
			if(start+1 == i -1)
			{
				cout << a[start+1] << a[i] << endl;
			}
			else
			{
				FindLargest(a,start+1,i);
				
			}
			FindLargest(a,i, n);
			return i;
		}
		else
		{
			FindLargest(a,start+1,n);
		}
	}
	else
	{
		return -1;
	}
}


void PrintArray(int *a, int n)
{
	cout << "Array" << endl;
	for(int i = 0; i < n; i++)
	{
		cout << a[i] << " " ;
	}
	cout << endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
	int a[]={2,8,4,1,6,9,3};
	PrintArray(a,7);
	FindLargest(a,0,7);
	return 0;

}

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

This is solvable in O(N) time and O(1) space.

M=N
for i = N-1 to 1
if(a[i+1] > a[M])
M=i+1
maxright[i]=M

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

stack containing those elements whose closest greater element yet to be found. unprocessed elements
1st element pushed
Iterate over to get next greater if(next greater element found) --> print pair. Pop the element from stack as its pair foun
push current value in stack so that current element pair can be found

NGE()	
	for each i from 1 to n-1
		while(a[i] > pe)
			print pe --> a[i]
			if(stack is empty)
				break
			s.push(a[i])	
			pe = s.pop()
		s.push(a[i])
		if(pe)
			s.push(pe)

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

1. initialize max to array[n-1]
2.traverse the array element from the right n-2 index
3.update each element to its max


think this would work in O(n) time:)

pls correct me if i am wrong.......

- ram rs August 01, 2013 | 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