Amazon Interview Question for Software Engineer in Tests






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

O(1)

- AL July 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

O(N)

- addy July 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

why is it o(1). shouldn't it be o(n)?

- random July 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

why is it o(1). At least all leaf nodes should be compared. The number of leafs should be around n/2, which lead to the cost as o(n)

- jproboy July 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

it will be O(lon n) because you would be comparing only half the elements becasue the smallest element would be present in the leaf node only.

- Shweta Singh July 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

it will be O(lon n) because you would be comparing only half the elements becasue the smallest element would be present in the leaf node only.

- Shweta Singh July 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

So, half of n is logn? :-)

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

searching in for min in n/2 elements is log(n/2) which is O(log n). log n looks to be correct

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

Let number of elements to be checked is n which is 16 = 2^4; m=4

dividing the long list in the following list of smaller elements.

With first scan (1st comparison ) - 2 * 16/2 = 2 list of 8 elements
With first scan (1st comparison ) - 4 * 16/4 = 4 list of 4 elements
With first scan (1st comparison ) - 8 * 16/8 = 8 list of 2 elements
With first scan (1st comparison ) - 16 * 16/16 = 16 list of 1 elements

In the fourth scan we will get the number.

Summing them together. 2*n/2+4*n/4+8*n/8+16*n/16 = n(1+1+1+1)= n * m (m =4)

Total scan needed = m . 2^m=16 taking log to the base 2, we have m = log (16)=log(n)


Conclusion- log(n) seems to be correct.

- rahul Sharma July 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

there is n/2 leafs   and they are not sorted we make a linear search in these n/2 elements ....O(n)

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

there is n/2 leafs   and they are not sorted we make a linear search in these n/2 elements ....O(n)

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

If log n were the answer in your example you should be able to get the min in log 16 = 4 comparisons.

- Kumar July 31, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

In a max heap every node is the minimum of its 2 children. So the minimum has to be a leaf. If we exchange any leaf(f) with this minimum leaf(mf), mf will never bubble up though f may bubble up. This suggests that mf can occur as any leaf(no pirticular position can be fixed for mf). Unlike BST we have no way of searching for an element in a binary heap. So we will have to do a delete_max() operation n times(may be n-1) to find the mf. Which means time taken to find mf is O(n). Is my argument correct.

- Dr.Sai July 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

jkjsd

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

there is n/2 leafs in a heap ...among which min exists...these are not sorted ..so we make a linear search among these leafs hence complexity O(n)

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

Yes, the complexity is O(n).

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

complexity will be log(n) below is my algo.

if(root->left==0 and root->right==0) return root->data;
else if(root->left && !root->right) root=root->left;
else if(root->right && !root->left) root=root->right;
else 
{
    rData=root->right->data;
    lData=root->left->data;
    if(rData<lData) root=root->rData;
    else root=root->lData;
}

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

wrong !!!

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

yaaa it should be O(n/2) which is O(N)

- kamal July 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

binary heap can be stored in an array - so the max for max-heap is array[0] and min for max-heap is array[last] - hence O(1)

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

it should be O(n/2) which is O(N)

- Anonymous August 01, 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