Google Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

In a binary tree the value can go anywhere as long as each node in resulting tree has between 0 and 2 children. In this question, BST is most likely meant. The choise where to place value equal to a given node in BST is up to the implementation. One may decide that the "left side is less than or equal to" while right sid is "more than" or the other way around.

- Anonymous November 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think there would not be any definite guideline for this but probably if there is duplicate there would be complication in searching & inserting.

- binhngoc17 November 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In Binary tree it does not matter your can insert the node any where, but it is BST i.e. Binary search tree then it has satisfy the Binary search property that left child key has to be smaller then root and right child key has to be larger then root.
duplicate key is not allowed in BS Tree. because when you want to search a node you will always hit the first occurrence so the second node with the same key will have no value.
I wrote functions for doing it
/**
* Function: insert_rec
* Input : root node and the data to be inserted
* Output : Tree with the inserted node
* Author : Qamar Alam (Vasil Technologies)
* Description: recursive function to insert a node
*/

TreeNode* Tree::insert_rec(TreeNode* ptr, int x) {
size++;
if (ptr == NULL) {
ptr=new TreeNode(x);
return ptr;
}
if(x < ptr->getDataInt()) ptr->left=insert_rec(ptr->left, x);
else if (x >ptr->getDataInt()) ptr->right=insert_rec(ptr->right, x);
else {
cout << "cant have two nodes with same key"<<endl;
size--;
}
return ptr;

}

/**
* Function: insert
* Input : root node and the data to be inserted
* Output : Tree with the inserted node
* Author : Qamar Alam(Vasil Technologies)
* Description: non recursive function to insert a node
*/

void Tree::insert(int x, int y) {

root = insert_rec(root, x);
return;

// to use non recursive comment the above two lines

TreeNode* ptr=root;
size++;
// if tree is empty then make it a root node
if (ptr == NULL) {
ptr=new TreeNode(x);
root=ptr;
return;
}
//find the place where it can be added as leaf node
while (ptr != NULL) {
if( x < ptr->getDataInt() ) //search the place in the left sub-tree
{
if (ptr->left == NULL)
{
ptr->left= new TreeNode(x);
return;
}
else
{
ptr=ptr->left;
}
}
else if(x > ptr->getDataInt())// search the place in the right sub-tree
{
if (ptr->right == NULL)
{
ptr->right= new TreeNode(x);
return;
}
else
{
ptr=ptr->right;
}
}
else //key exist in the tree and error out or ignore
{
cout << "cant have two nodes with same key"<<endl;
size--;
return;
}
}
return;
}

- Anonymous January 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

Yes,

Binary tree can have duplicates where as Binary Search Tree (BST) cannot. However, Binary tree cannot have both child node values equal at the same level.

Inserting a element which is equal to root node value, it will put the value into left child first.

e.g.
5
/ \
3 7
/ /
3 7

- Pankaj Gadge November 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Its a binary tree not a binary "search" tree. It does not matter what and how you insert. All that binary tree guarantees is that every parent has two children. So inserting duplicates on left or right does not matter.

- axecapone November 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Actually BST can allow duplicates.

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

So Axecapone,

Can duplicates be allowed in a binary search tree? If yes, why , if no why? Please explain.

- sivaji8 November 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

In binary search tree, duplicate will inserted as the right node of the node which is having same value.

- Anonymous November 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Why couldn't a BST have duplicates? Even if your existing BST design did not allow for them, you could easily accommodate duplicates simply by giving each node an integer that would store a count.

- eugene.yarovoi November 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

http: //cslibrary.stanford.edu/110/BinaryTrees + ".html"

I really dont know why can't we have duplicates.
Stanford will prove it for others who are confused.

- Scofield November 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

http: //cslibrary.stanford.edu/110/BinaryTrees + ".html"

I really dont know why can't we have duplicates.
Stanford will prove it for others who are confused.

- Scofield November 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Scofield: I'm not sure what you're referring to on that page. Are you arguing for or against duplicates? If against, I've given a rather ironclad argument above for why we can rest assured that we can make a design that accommodates duplicates.

- eugene.yarovoi November 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

More than a given definition, .. real life business use cases matters .. and duplicates in BST is a common scenario ... so we allowed it.

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

Extending the response from ‘axecapone’, I think too that binary tree is just a representation of parent child relationship and thus duplicate key is most likely unnecessary. However if a duplicate key in a particular case tends to break the invariant of parent child relation in that case each node should maintain collection of values corresponding to colliding keys (keys with same value). If for a non-duplicate binary tree, a node is represented by three parameters – leftChild, rightChild and value then with duplicate keys a node should be modified to hold a collection of values. In addition appropriate data structure such as linked list or set or the likes should be used for holding collection of values corresponding to duplicate keys.

- buckCherry November 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Whoever has voted this response down, I would appreciate a reasoning so that I can correct myself in future.

- buckCherry November 07, 2012 | Flag
Comment hidden because of low score. Click to expand.


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