Amazon Interview Question for SDE1s


Country: United States
Interview Type: Phone Interview




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

People doing inorder traversal won't get the job. Imaging a perfectly balanced tree of a million nodes...

Answer is simple: Implement predecessor and successor.

Cost will be O(log n) for balanced trees for find the 3rd closest.

For generic k, this will be O(k log n), but for larger k, this will become O(n) (amortized successor cost is O(1) actually).

- Anonymous May 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sounds good... But I too approached in the same way at first but later switched to a inorder kind of solution since no where in the question it was mentioning about a height balanced bst.. the cost can be O(log n) only in that case isn't it... Anyway, in case given bst is height balanced , we should take advantage of that... :-)
Thanks,

- ran May 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@ran. Even if the tree is not height balanced, the inorder based complete traversal solution will be quite bad compared to this.

Only in the extreme cases where the tree is a single path or something like that would that be better.

- Anonymous May 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yea... U r right...And as i said earlier at that moment i just overlooked that aspect n switched frm my predecessor/successor soln n it cost dearly for me ...

- ran May 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Implementing predecessor and successor is an awesome idea in case if you have parent pointer. Now try to solve it without parent pointer :)

- gevorgk July 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

The optimized solution is to maintain a max heap of size 3 later extend it to 'k'. Where max is the difference between the element and the value. The time complexity will be:

O(N) tree traversal + O(k) heap creation + O(logk) hepify = ~O(N).

Comments are welcomed.

- Nascent May 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Is it max heap or min heap

- dadakhalandhar May 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

- maintain a 3 element sorted queue.
- as the BST is traversed (O(n)), if the difference is less than the max element, push the new one at the proper place in the queue (const time operation)
- at the end, the largest element in the queue is the solution.
- total cost is O(n)

- theblackboa May 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Or use a 3 element priority queue.

- Anonymous May 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Simple Solution:

1. Start Root -> Right in the In-Order, Find the Kth Value. -- K1
2. Go to Root->Left
3. Start Traversing in 'In-Order Reverse' (Right - Root- Left), Find the Kth Value -- K2
4. Answer : K1 > K2 ? K2 : K1

- Anonymous May 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bst does not seem to make the soultion more efficient

- Jackie May 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

There is no need to traverse whole BST.
Simple solution:
1. make a circular queue of size k that will hold the tree node type pointers.
2. Now search the value in the BST which will be in time O(log n) complexity.
3. While searching the value in tree just enque parents in the circular queue.
4. When u find the node at that time u'll have a queue that is holding upto kth parent.
5. Now compare values of the following nodes from the queue {kth, child of kth-1, child of child of kth-2, and so on upto the node we were searching}
6. U'll find the value of kth closest no.

- nitin.sharma4959 May 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Integer[] closest = new Integer[3]; // initialized with null  
void find_3rd_closest(Node current , int K){

	Node succ = Successor(current);
	Node pred = Predecessor(current);
	
	insert(closest , current.val , K);
	if (succ != null)
		insert(closest , succ.val , K);
	if (pred != null)
		insert(closest , pred.val , K);
		
	if (succ != null && pred != null)
		if (Math.abs(pred.val - K) < Math.abs(succ.val - K))
			find_3rd_closest(pred , K);
		else
			find_3rd_closest(succ , K);
	else if (pred != null)
		find_3rd_closest(pred , K);
	else if (succ != null)
		find_3rd_closest(succ , K);
	else
		return;
}

void insert(int[] closest , int val, int K){
	for (int i = 0 ; i < closest.length ; i++){
		if (Math.abs(K - val) < Math.abs(K - closest[i])){
			for (int j = i ; i < closest.length - 1 ; i++){
				int temp = closest[i+1];
				closest[i+1] = closest[i];
				closest[i] = temp;
			}
			closest[i] = succ.value;
		}
	}
}

Node Successor(Node current){
	if (current.rightChild == null)
		return null;
	current = current.rightChild;
	while (current.leftChild != null)
		current = current.leftChild;
	return current;
}

Node Predecessor(Node current){
	if (current.leftChild == null)
		return null;
	current = current.leftChild;
	while (current.rigthChild != null)
		current = current.rightChild;
	return current;
}

- Anonymous June 08, 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