Samsung Interview Question for Senior Software Development Engineers


Country: United States
Interview Type: In-Person




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

Can be done by Inorder traversal technique, which will result in O(N) time.

Another Approach:
Let each node in the BST have a field that returns the number of elements in its left and right subtree. Let the left subtree of node T contain only elements smaller than T and the right subtree only elements larger than or equal to T.

Now, suppose we are at node T:

k == num_elements(left subtree of T), then the answer we're looking for is the value in node T
k > num_elements(left subtree of T) then obviously we can ignore the left subtree, because those elements will also be smaller than the kth smallest. So, we reduce the problem to finding the k - num_elements(left subtree of T) smallest element of the right subtree.
k < num_elements(left subtree of T), then the kth smallest is somewhere in the left subtree, so we reduce the problem to finding the kth smallest element in the left subtree.

This is O(log N) on average (assuming a balanced tree)

int kSmallestInBST(NODE *root, int k) {
	int count;
	count = sizeOfBST(root->left) + 1;
	if( k == count)
		return (root->data);
	else if( k < count)
		return kSmallestInBST(k,root->left);
	else
		return kSmallestInBST(k-count,root->right);
}

int sizeOfBST(NODE *root) {
	if(NULL == root)
		return 0;
	else
		return (sizeOfBST(root->left) + 1 
		        + sizeOfBST(root->right));
}

- R@M3$H.N September 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think the efficiency of the above algorithm is not O(log N).

There is are two recursive calls.
1) the function sizeOfBST is O(N log(N)) since it traverses the entire tree for each half.
2) the function kSmallestInBST is O(log N) since it goes to each half of the tree.

and point 1) is called as many times as 2) and hence
Efficiency is O(n * logn * logn)

Let me know if I am wrong. I didnt do detail analysis, just a glance of the code.

- Sayantan Samanta September 24, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Agree

- R@M3$H.N September 25, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think, you are not saving anything here apart from some constant save,
Calculation of number of nodes on the left subtree is O(number of elements of left subtree), which is equal to accessing the value of the element, i think in every case it is going to be O(n) more appropriately O(k)

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

what if we store the size in tree node we can save sizeofBST recursive calls

- nikhil September 26, 2014 | 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