Google Interview Question for Software Engineer / Developers


Country: United States




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

There is no in-order successor for the node with value 6.

- Jayasraj October 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 votes

I have come up with a solution for finding the in order successor when the links to parents are not provided. Could you check if this is right? I already checked. I would feel confident if someone cross checks it.


public Node inOrder(Node n, Node root){
if(root == null){
return null;
}
Node successor;
Node max;
while(root != null && root != n.data){
Node parent = root; // 4 // 6
if(root.left != null && n.data <= root){
if(root >= max){
max = root; // 6
}
root = root.left; //5
successor = parent; //6
}else if(root.right != null && n.data >= root){
if(root >= max){
max = root; //
}
root = root.right; //
successor = parent; //
}
}

if(root.right == null && root == parent.left){
return max;
}

if(root.right != null){
return leftMostChild(root);
}

}

public Node leftMostChild(Node n){
Node current;
if(n.right != null){
current = n.right;
}
while(current.left != null){
current = current.left;
}
return current;
}

- Gaile October 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

This should do the job (or the bugless version of it)....

void succ(Node x, int key)
{
	static bool found;
	
	if(x==nil) return;

	succ(x.left);

	if(x.val == key) found=true;
	else if(found) printf(x.val or x), found=false, return;

	succ(x.right);
}

- Urik's twin Cookie Monster (dead now) October 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

void succ(Node *x, int key)
{
    static bool found;
    
    if(x==NULL) return ;

    succ(x->left);

    if(x->val == key) found=true;
    else if(found) cout<<x.val or x<< "is successor", found=false, return;

    succ(x->right);
}

In search trees, you can cut the complexity by following the actual path down, noting the last "possible" successor ancestor, then try diving below the target node to find a successor, else use the ancestor.... (works on paper)... but blah blah blahhhhhhhhhhhhhhhhhhh that's boring.

- Urik's twin Cookie Monster (dead now) October 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

there is no inorder successor

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

@algos interpretation of the problem is very smart. However, if this question is concern with the actual structure of the given BST, then we can use something like the code below:

struct bst* InorderSuccessor(struct bst *b, struct bst *value)
{
	if(tmp->lc!=NULL)				
		return tmp->lc;	
	else if(tmp->rc!=NULL)	
		return tmp->rc;			
	else if(tmp==tmp->parent->lc && tmp->parent->rc!=NULL)
		return tmp->parent->rc;
	else
	{		
		struct bst *tmp1=tmp->parent;						
		while(tmp1->parent!=NULL && (tmp1->rc==NULL || tmp1->rc==tmp))
		{
			tmp=tmp1;
			tmp1=tmp1->parent;				
		}
		if(tmp1->rc!=NULL)
			val=tmp1->rc;
	}
	return NULL;
}

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

Sorry, my mistake! I forgot to include the following two lines:

if(b==NULL)
  return NULL;
struct bst *tmp=b;

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

inorder travesal always results in sorted values and there is no vlaue grt than 6 . means no successer

- alok.net October 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

No in order successor to 6, and here is my solution.

public class BSTinorder
{
	// Return the in order successor of a given node in a BTS
	
	public class Node {
		public Node right;
		public Node left;
		public float data;
		public Node(float data, Node left, Node right) {
			this.right = right;
			this.left = left;
			this.data = data;
		}
	}

	private Node root = new Node(4,
			new Node(2,
				new Node(1, null, null),
				new Node(3, null, null)
				),
			new Node(6,
				new Node(6,
					new Node(5,
						new Node(4.5f, null, null),
						new Node(5.5f, null, null)
						),
					null
					),
				null
				)
			);
	
	public Node after(float value, Node node) {
		if (value >= node.data) {
			return node.right == null ? null : after(value, node.right);
		} else {
			if (node.left == null) {
				return node;
			}
			Node afterLeft = after(value, node.left);
			if (afterLeft == null) {
				return node;
			}
			return node.data < afterLeft.data ? node : afterLeft;
		}
	}
	
	public static void main(String[] args)
	{
		BSTinorder instance = new BSTinorder();		
		Node node = instance.after(Float.parseFloat(args[0]),instance.root);
		if (node == null) {			
			System.out.println("no successor");
		} else {
			System.out.println(node.data);
		}
	}
}

- bebert.dude October 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The logic that I would put into this question would be sth like this:

if(root)
{
InOrderSuccessor(r->left)

}

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

The logic that I would put into this question would be sth like this:
Node * InOrderSuccessor (Node * r, int data)
{
if(root)
{
InOrderSuccessor(r->left, data)
if(r->data > data)
{
return r;
}
else
{
InOrderSuccessor(r->right, data);
}
}
}

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

This code works fine, returns null if there is no inorder successor.

Node * InOrderSuccessor (Node * r, int data)
{
	if(r) 
	{ 
		InOrderSuccessor(r->left, data) 
		if(r->data > data)
		{
			return r;
		}
		else
		{ 
			return InOrderSuccessor(r->right, data); 
		}
	}
	else
	{
		return null;
	}
}

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

why don't we just do in order traversal of the tree and store it into an array, then we just simply find the next item after the node we are interested. If null then means it doesn't have an in order successor. time O(n), space O(n). I guess the only penalty here is the extra memory space used.

- david86391 October 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

the non-space penalty solution could be:

int _iOS(Node *n) {
	if(n.left == null) {
		return n.value;
	}
	_iOS(n.left);
}
//node is the object which we want to find it's in order successor
int inOrderSuccessor(Node * node) {
	if(node.right == null) {
		return null;
	}
	return _iOS(node.right);
}

- david86391 October 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is the is the Inorder sequence:
1,2,3,4,4.5,5,5.5,6

- Rich October 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In order successor in a BST will be the smallest number greater than the given number.
Which would be the left most node in its right subtree.

def in_order_successor(node):
    if node.right is None:
        return None
	temp_node = node.right
	while temp_node.left != None:
		temp_node = temp_node.left
	return temp_node

- pv November 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

as per the sequence the possible in-order successor could be 7. if you need to find the in-order successor with the existing data then return -1 as that doesn't exist

---------------4
-------2--------------6
----1----3-------5-------7
--------------4.5--5.5
4 is average of 2&6
2 is average of 1&3
5 is average of 4.5&5.5
6 is average of 5&7

- algos October 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

There is no 7 in the BST. 6 has just left child and no right child. Now what would be the answer?

- Gaile October 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

There is no 7 in BST. 6 has only left child 5 and no right child. Whats the answer now?

Why can't 5 be the in order successor? Because it is the next largest after 6 in BST

- Gaile October 22, 2013 | 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