Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

in the web, i seen a new DS called Heap Structured Search Trees. I don't know this is the right answer. you can check if interested.

http : // home.tiac.net/~cri/2008/traversal.html

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

i thought about combining heap and BST properties, but didnt knew it was a standard thing... :)

- Parixit November 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes, it is a good data structure, but it also has same problem which we have for BST, so avg. case complexity is O(log(n)). and it is still a heap, but i think here the question is a heap is already given to you, now you tell....

- sonesh November 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Use a companion hashtable/tree.

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

Wouldn't it take O(n) time to create the companion hashtable?

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

Insert a heap itself is an O(logn) operation, and a binary tree traversal is also an O(logn) operation. So first pass, travel the heap to see if theres a duplicate. If not, insert it. That is O(2*logn). Don't know its allowed.

- Leo November 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Its not clear how the traversal will be of O(logn) because this is not a binary search tree. the number could be in either subtree. Thanks

- artemis November 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@artemis - heap is also full binary tree, in other words tree representation of array

check chapter 6 in CLRS book

- siva November 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@siva: Artemis is right in saying that searching an element would be a O(n) operations. Though its a binary tree (normally), but heap node's subtrees won't have BST property of being > than left and less than the right subtree. Hence Binary search won't happen. One optimization which can be done is to check for n-1/2 elem value (assuming its implemented as array), and going to leaves or internal nodes hence. Other can be (assuming max heap), to stop going in the subtree if the node's values is less than the element being searched. But, still it'll be O(n)....

- Prashant November 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Prashant..yes, exactly what I meant.
@siva..searching in binary tree and binary search tree are different, because BST has the property of left node<root and right node>root, whereas in case of heap we if the element we are searching for < root then it can be in either left/right subtree.
Also, I will add..this was a theory question, so it may not be possible...but I am not sure.

- artemis November 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You need some sort of supplementary data structure. It can't be done if all you have is a heap.

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

Yes, I thought so. Thanks

- artemis November 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Artemis: we can in fact show that this is so. Consider a heap whose bottom most layer is full (about n/2 elements there). Suppose that all the elements in all levels except the bottom level are larger than the number we're looking for. Then, the element we're looking for can be anywhere in the bottom-most level. We have to search in the bottom-most level, but checking any position in the bottom-most level reveals no information about any other element in the bottom-most level. In other words, if there's any position in the bottom-most level that you don't check in this particular situation, the element you're looking for could be hiding there. So in the worst case here, you must check every one of n/2 positions in the bottom-most level, which takes O(n) time.

- eugene.yarovoi November 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 4 vote

we can take an extra array which stores the index of heap elements (heap is implemented using array) in sorted order,it will be kind of hashing concept. now applying binary search in this array taking the indices for the heap array - O(logn). and then, if not present, insertion in heap - O(logn)..

- Parixit November 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Huh?

You also need to update the sorted array on a heap insert, which makes insert Omega(n).

There was a reason others were talking about trees/hashtables.

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

along with heap insertion, the sorted index array will also be updated accordingly

- Parixit November 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Parixit: Please elaborate on how you will update the sorted array in O(log n) time.

- Anonymous November 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

during heap insertion, when the elements need to be shifted, then there only, the index-array elements will also be shifted

- Parixit November 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Huh?

How will you insert in the middle of an array in O(log n) time?

- Anonymous November 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

(I am talking about the array which is sorted).

- Anonymous November 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

for example..

max-heap : 99(0), 77(1), 88(2), 11(3), 33(4), 22(5), 55(6)

correspoding index array : 3(0), 5(1), 4(2), 6(3), 1(4), 2(5), 0(6)

- Parixit November 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

why you guys are making the problem too difficult. take an avl tree, and insert element both in heap and avl tree, now before inserting an element in heap, find out(in (logn)) weather the element is present or not, if not, insert in into both(require O(logn))

- sonesh November 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@sonesh... Do u even know the difference between HEAP and AVL???
Please find out more about AVL before making such random statements.

- Bevan November 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Parixit: Now when you insert say 50 into the heap, how will you update the index array?

- Anonymous November 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Bevan: LOL. You are a moron.

- Anonymous November 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

after inserting 50
max-heap : 99(0), 77(1), 88(2), 50(3), 33(4), 22(5), 55(6), 11(7)
correspoding index array : 7(0), 5(1), 4(2), 3(3), 6(4), 1(5), 2(6), 0(7)

- Parixit November 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Parixit: This is getting tedious, as you don't seem to be getting the point.

Now explain how you modified the index array in O(log n) time.

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

add the element at the end of heap and increment the size of heap
check if it is great than its parent, if no, break
if yes, swap the element with its parent, repeat the above procedure until the element is already at the root or less than its parent

- dgbfs November 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

it can be done, just think about heap in terms of segment tree

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

@lfenjoy9: you've described the normal heapify process. You haven't discussed the key aspect here of how you would ensure the element is not already in the heap.

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

How about using a hash table and looking it up?

- Shivku November 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Shivku: that would work, except then you're using more than just a heap.

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

How can we handle duplicate keys using hash ?

- Coder February 09, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

this works in java

1.   Add  all items in Hashset() 
2.  Now try to add new item in Hashset() , 
     If HashSet returns false ( means duplicate is present )  then you don't need to do anything
    If HashSet()  returns true then insert item as usual in heap

- RK December 01, 2012 | 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