Oracle Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

Do an inorder traversal, stopping when come to a larger key.

Possible pseudo-code

void printSmaller (Tree *root, int k) {
    if (!root) return;
        
    printSmaller(root->left, k);
    if (root->data < k) {
        printf("%d ", root->data);
        printSmaller(root->right, k);
    }
}

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

I guess, you can optimize this code to some extent.
The idea is if root element is less than key, then certainly all left child's element will have lesser value.
Pseudo Code:

printAll(node *root){
printAll(root->left);
printf("%d",root->data);
printAll(root->right);
}

print(node *root,int key){
if(root==null) return;
if(root->data <=key){
printAll(root->left);
printf("%d",root->data);
print(root->right,key);
}else{
print(root->left,key);
}
}

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

Good optimization.

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

how is it better or optimized .. u have to traverse through all elements anyway right which are smaller than the key ???

- AJ October 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 3 vote

Just INORDER-WALK its left child.

- lmykyo August 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Not quite. If you're in a right subtree, everything above the subtree and to the left is smaller. So walking only the left child is incomplete.

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

This is so wrong. Idiots with voting power. The joys of democracy...

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

o traverse to the key element
o if the element is a left-child(node->parent->left == node), then traverse the node->left subtree
o if the element is a right-child(node->parent->right == node), then traverse the node->parent->left subtree followed by node->parent followed by node->left subtree.
<will post working code soon>

- Sai August 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The idea is simple. As you move right in the BST, in-order print the left subtree and the current node. As you move left in the subtree, print nothing since the search key is bigger than the current node key. If you find the key, print the left subtree and return.

I do a find() first so if the key doesn't exist, I simply return and print nothing. I guess that increases the running time by a constant factor and I'm sure it can be optimized.

def walkLeft(self, inputKey):
current = self._root

if self.find(inputKey) == None:
return

while current != None:
currentKey = current.getKey()
if inputKey == currentKey:
self.walkInOrder(current.getLeft())
return
elif inputKey > currentKey:
self.walkInOrder(current.getLeft())
print str(current.getKey()) + ',' + str(current.getData())
current = current.getRight()
else:
current = current.getLeft()

- Dan August 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

find(root,key)
{
      if(root == NULL) return;
      
      if(root <= key)
      {
             print_all_left_nodes(root);
             find(root->right,key);
      }
      else
             find(root->left,key);
}

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

Here is a Java version:

public void printAllValuesSmallerThen(Node node, Integer value) {
		if (node == null)
			return;
		
		this.printAllValuesSmallerThen(node.getLeftChild(), value);
		if (node.getData() > value)
			return;
		else
			System.out.print(node.getData() + " ");
		this.printAllValuesSmallerThen(node.getRightChild(), value);		
	}

- gonzo August 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

void printsmall(tree *node,int data)
{
if(node)
{
printsmall(node->left,data);
if(node->item < data)
printf("%d ",node->item);
printsmall(node->right,data);
}
return;
}

- sandeep sharma August 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

void print_till(mynode* root, int elt){
if(root){
print_till(root->left, elt);
if(root->value<=elt){
printf("%d ", root->value);
print_till(root->right, elt);
}
}
}

- Avenger August 29, 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