xyz Interview Question for abcs


Country: India




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

InOrder traversal. But counter starts incrementing after you reach the left most node (including it).
Return current node when the counter is equal to n.

- zr.roman February 06, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This will work for BST only. But we have just the regular binary tree.

- Anon February 07, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

can plz u elaborate with example how ur algo works in binary tree (its not bst )

- abhay0609 February 07, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think the author meant BST.
just "binary tree" is senseless, because he could just write "given an unsorted array".

- zr.roman February 08, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I agree with zr.roman, additionally we can speedup the search if we can precompute and store the number of nodes in the left and right subtrees for each node

- pavel.em February 14, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Walk thru a tree and insert each element into a min heap.

Remove n elements from the heap to get the n-th smallest.

- Anon February 06, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Put all elements from the tree to a min heap. Remove n to get the nth smallest.

- Anon February 06, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using a max-heap (instead of a min-heap) is faster. insert the first n elements from the tree to the heap, then for each additional element, if it is higher than the top of the heap, pop the heap and push the new element. After inserting all elements, we have the nth smallest item on the top of the heap. The time complexity is k log n (where k = # items in tree) vs k log k.

- Anonymous February 08, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your solution seems clever, but it does not give any advantage, asymptotically speaking. here is why, your time complexity is: k log n, as max_heapify() takes log n for each (k - n) node. however, k log n is O(k).

if follow min-heap, it takes O(k) to build the min heap. And then, it will take n times log k to find the n-th smallest element, where each extraction of smallest element takes log k to max_heapify. So, it's k + n log k, which is still O(k).

but, as I said, asymptotically speaking ... you still have a great angel

- Sean February 11, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

in case of arbitrary binary tree without any order of nodes the algorithm is the following:
1) re-arrange nodes to build a min-heap (in-place due to just re-arrangement of nodes). O(n).
2) k times call extract-min O(1) and min-heapify O( k log n).
Worst case is O(n log n) in case the kth smallest element is the greatest one by coincidence.

- zr.roman February 09, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public int findKthSmallest(BinaryTreeNode<Integer> root, int k) {
		if (null == root)
			return -1;
		int n = BinaryTree.size(root);
		int a[] = new int[n];
		inorder(root, a);
		Arrays.sort(a);
		return a[k - 1];
	}

	int i = 0;

	public void inorder(BinaryTreeNode<Integer> root, int[] a) {
		if (root != null) {
			inorder(root.left, a);
			a[i++] = root.data;
			inorder(root.right, a);
		}
	}

- Raj February 07, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Iterative version

public int kthSmallest(TreeNode root, int k) {
            int h = 0;
	        Stack<TreeNode> stack = new Stack<>();
	        TreeNode current = root;
	        while (!stack.isEmpty() || current != null) {
	            if (current!= null) {	        
	                stack.add(current);
	                current = current.left;
	            }
	            else {               
	                	h += 1;	                	
	                	current = stack.pop();
	                	if(h == k)
	                		return current.val;	                                    
	                    current = current.right;	                
	            }
	        }
	        return 0;
    }

- EPavlova February 07, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Given is binary tree not binary search tree. So, We do not have a way to tell inorder traversal give sorting sequence

- Raj February 09, 2016 | 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