Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

B+ tree can be use..

- researcher knowledge September 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i agree

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

Could you explain your solution a little more

- Anand_friendly84 January 09, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 4 vote

You have to use Segement Trees for this.
If you clearly read the problem, the interviewer wants to get the minimum and maximum element being tracked from a particular node.
This is what segment tree does.

- cksharma.skt September 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Here by I am writing the code for implementing the segment from the series of number...

Suppose I have int[] arr = {1, 2, 3,4, 5, 6, 7, 8};
I will take one more array of size int[] tree = new int[3 * arr.length];

And I have to keep track of maximum\ value from each node forming the segment trees,

public void buildSegementTree(int n, int startRange, int endRange)   {
    if (startRange > endRange) return;
    if (startRange == endRange) {
        tree[n] = arr[startRange]; // n the index in tree keeps track of the maximum value. (when range is 1 .. ::)
        buildSegementTree(n, startRange, (startRange + endRange) >> 1);
        buildSegementTree(n,  (startRange + endRange)  / 2 + 1,
        endRange);
        tree[n] = Math.max(tree[2 * n], tree[2 * n + 1]);
    }
}

This way each node is keeping track of the maximum value in the down tree... Similary do for minimum value. And querying which will happen in log(n).

- cksharma.skt September 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

this is close to what the interviewer was hinting on...
btw do u know any book for trees and different type of trees and problems on trees?

- scorpionking September 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ya CLRS , Introduction to Algorithm covers these stuffs. And geeksforgeeks.org also has some materials about this.

- cksharma.skt September 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

good stuff esp the geeks for geeks site

i found this very informative
ardendertat .c om/2012/01/09/programming-interview-questions/

- scorpionking September 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

but with segment trees, after every addition and deletion, the segment tree will have to be updated.
So these 2 operations will be O(n)
What am I missing here?
How can we update the segment tree after insertion/ deletion?

- Tushar September 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If you are updating a value (updating at particular index) in given array). this adjustment can be done in O(log(n)) in the segement tree .. Same is the case for deletion of single element.

public void updateSegementTree(int n, int start, int end, int index, int val) {
	if (start > end || start > index || end < index ) return;
	 if (start==end) //at leaf node
	 {
	  tree[n] = val;
	  return;
	 }

	 updateSegementTree( n*2 , start , (start + end ) / 2 , index, val );
	 updateSegementTree( n*2 + 1 , (start + end) / 2 + 1 , end , index, val );
	 tree[n] = max( tree[n*2] , tree[n*2 + 1] );
}

- cksharma.skt September 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

scorpion king is a f***ing moron. No wonder he didn't get an offer. After being repeatedly asked for clarification, he still does not give any.

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

@Anonymous: True that. The movie sucks too.

- Anonymous September 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

LinkedHashMap

- tangjian September 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I don't think it's clear if it was stated as the original poster phrased it:

"optimum addition removal, querying and priority." is broken english at best.

One might suspect he was "trying" to say: "optimal (addition removal), (querying), and (priority)" or:
optimal (addition removal)
optimal (querying)
optimal (priority)

Only the second makes sense. Optimal "addition removal" doesn't mean anything in this context nor optimal "priority."

When you state a problem, it should be VERY clear if you want a meaningful answer.

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

I think he means add, delete, search, and find the min or max. A balanced BST can do all of these in logN time, if we maintain two pointers to the left-most leaf node (min) and the right-most leaf node.

- nsu October 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

what about in a node of BST we just keep the size of the left subtree and right subtree, so we can get priority target within O(log(n)).

- Yuming October 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Please elaborate on what addition, removal, querying and priority mean.

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

yeah ..pls elaborate the question ....

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

What do you mean by: store the min and max child?
How would you retrieve say- element with 3rd highest priority in logN?

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

Ok. Saw the edit. Now tell us what the actual question is!

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

I think Hash Table probably is the best for optimal perf on Insert, Removal and Lookup (all O(1) amortized). however priority is not a strong suit of Hash Tables and you would either need some kind of object as your value, or perhaps use the Key to generalize the priority ?
I guess you never know what interviewers are actually looking for with questions like this

- Joe Coder September 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Yes it is BST because
Heap BST Hash
Insert log n log n p (where p is the max length of linked list)
Delete log n log n p
Priority O(1) log n p
Query O(n) log n p

I hope the complexities are correct.

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

Well i think we can use fibonacci heap...

- Apoorv September 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This question doesn't make any sense.

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

maybe a doubly linked list?

- Hitman September 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 votes

hmm OK after a long list of offensive comments which tried to justify my rejection, let me try to explain again:
1. It was an open ended question at the end, He was not having a special case..
He precisely asked me this:

I want a data structure which gives me efficient lookup, insert and querying by priority (i want the second biggest element, 5th biggest,etc)
He also mentioned that I am free to use my creativity and not limit to the standard data strucutres.

First i said hash, he said hash will not give efficient priority lookup

Then i said linked Hash, which too he rejected
BST also rejected.
Then i suggested Heap. He said heap will just give efficient lookup for the most prioritized node, not the 2nd most or 5th most

My issue was I did not read abt anything else than the basic data-structures.

He hinted that I could solve it using changes to basic BST. And the time was up, he was in hurry to leave for home as my interview was the last.

After calling off, he told me the solution which is the same as cksharma.skt suggested and only he could solve properly in this forum :)

My advise for other interviewees would be to atleast read a one liner about datastructures other than the basic ones.

- scorpionking September 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@scorpionking.

You are not explaining _again_. This is a _new_ explanation which was missing all along. The people asking for clarification (multiple times) are justified in being annoyed. Name calling is another matter.

Also, priority is a completely wrong word to use (especially when you give no more details than addition, removal, query and priority).

Was it so hard to explain it a bit more?

Also, if you don't understand why the question you wrote was unclear, well...

- Anonymous September 21, 2012 | 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