Google Interview Question for Software Engineer / Developers


Country: -




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

O(log n) is possible.

Split and join can be implement in O(log n) for AVL, red-black and variants.

- Anonymous July 31, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

//merge two BST into a balanced tree time O(n+m) space O(1)
//12345678
//          5   
//      3      7
//    2  4  6  8
//  1
public static class TreeNode {
 TreeNode left;
 TreeNode right;
 int data;
}

public TreeNode mergeTrees(TreeNode rootA, TreeNode rootB) {
 TreeNode sortedListA = treeToList(rootA, null);
 TreeNode sortedListB = treeToList(rootB, null);
 int[] refSize = new int[] {0}; // cheat for reference
 TreeNode mergedLists = mergeList(sortedListA, sortedListB, refSize);
 return sortedListToBST(new TreeNode[] {mergedLists}, 0, refSize[0]);
}

//return balanced BST
private TreeNode sortedListToBST(TreeNode[] refSortedList, int startIdx, int endIdx) {
 TreeNode root = null;

 if (startIdx < endIdx) {
  int midIdx = (startIdx + endIdx) >> 1;
  
  TreeNode left = sortedListToBST(refSortedList, startIdx, midIdx);
  root = refSortedList[0];
  root.left  = left;
  refSortedList[0] = refSortedList[0].right;
  root.right = sortedListToBST(refSortedList, midIdx, endIdx);
 }
 
 return root;
}

private TreeNode treeToList(TreeNode tree, TreeNode head) {
 if (tree == null)
  return head;

 TreeNode left = treeToList(tree.left, head);
 tree.left = left;
 
 return treeToList(tree.right, tree);
}

private TreeNode mergeList(TreeNode listA, TreeNode listB, int[] size) {
 TreeNode result = null;

 while (listA != null || listB != null) {
  size[0]++;

  if (listA == null) {
   result = addNodeToList(result, listB);
   listB = nextNode(listB, true);
  } else if (listB == null) {
   result = addNodeToList(result, listA);
   listA = nextNode(listA, true);
  } else {
   if (listA.data > listB.data) {
    result = addNodeToList(result, listA);
    listA = nextNode(listA, true);
   } else {
    result = addNodeToList(result, listB);
    listB = nextNode(listB, true);
   } 
  }
 }
return result;
}

private TreeNode addNodeToList(TreeNode list, TreeNode newNode) {
 if (list == null)
  return newNode;

  newNode.right = list.right;
  list.right = newNode;
  return list;
 }

private TreeNode nextNode(TreeNode list, boolean unlink) {
 TreeNode result = list.left;

 if (unlink)
  list.left = null;

 return result;
}

- GKalchev March 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Apart from solutions using linked list if u know any thing tell me...

- kumar June 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

no

- upadhyay.vivekranjan June 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sd

- sd June 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is some approach:

void insert(struct node* tree1, struct node* tree2) {
// remember the left and right sub-trees;
struct node* left = tree2->left;
struct node* right = tree2->right;
insertintoTree(struct node* tree1, tree2);
insertintoTree(struct node* tree1, left);
insertintoTree(struct node* tree1, right);
}
insertintoTree() {
// normal binary tree approach to insert into a balanced tree;
}

- SS June 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

pseodo code:
merging B in A.

void merge(rootA, rootB)
{
    merge(rootA,rootB->left);
    merge(rootA,rootB->right);
    mergeAndBalance(rootA,rootB);
}
void mergeAndBalance(rootA,rootB)
{
    rootB->left=rootB->right=NULL;
    /*insert rootB and balance the tree using rotations
      lot of code already available. 
    */
}

- Anonymous June 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

+ NULL check is required in merge.

- Anonymous June 01, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@above, I don't know why you are coming up with the same crap again and again..basically the above soln is not efficient which is already reported here.. why the hell you want to post it again that too with a second update ??

- Anonymous June 01, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

y r u getting annoyed man? if u have the better solution then post it instead of crying. then talk about efficiency.

- Anonymous June 01, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Actually this is much efficient solution as it does not require to create linked list which is additional memory.

- Ashish Kaila June 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Just a curiosity, how many of you really can produce bug-free code (in an interview) to do those fancy rotate operations used in AVL/RB tree?

- anon June 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

1) Two sorted arrays, just merge them to a 3rd sorted array using the merge routine from mergesort.
2) pass the combined sorted array into a method that will build the tree in a binary search fashion (ie, divide the sorted array in half, left side of the array will be the left side of the tree, right side of the array will be the right side of the tree).

- hiralg June 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You should think about whether building a BST from a list of sorted numbers such as 1, 2, 3, and 4 give you a balanced tree.

- Anonymous June 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It does. You are building the BST in a binary search fashion. You won't get an balanced tree because you are inserting from the middle. Read my approach the code that follows again.

- Anonymous June 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey20921" class="run-this">See my reply above:

public void buildTree(TreeNode[] nodes, int low, int high)
{
if (low > high)
return;

int mid = (low + high) / 2;

insert(nodes[mid]);
buildTree(nodes, low, mid-1);
buildTree(nodes, mid+1, high);
}

public void insert(TreeNode n)
{
if (root == null)
root = new TreeNode(n.data);
else
{
TreeNode curr = root;
TreeNode parent = curr;
boolean addingAsLeftChild = false;

while (true)
{
if (n.data > curr.data)
{
parent = curr;
curr = curr.right;
addingAsLeftChild = false;
}
else if (n.data < curr.data)
{
parent = curr;
curr = curr.left;
addingAsLeftChild = true;
}
else
{
//duplicates not allowed
return;
}

if (curr == null)
{
if (addingAsLeftChild)
{
parent.left = new TreeNode(n.data);
}
else
{
parent.right = new TreeNode(n.data);
}
return;
}
}
}
}
</pre><pre title="CodeMonkey20921" input="yes">
</pre>

- hiralg June 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

yo just convert both bsts to the DLLs in linear time and then merge the two linked list aagaina linerar time and then convert this dll to a bst again a linear time a nd constant space taken

- geeks July 03, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a) Find the number of nodes in each tree. treeA (n1), treeB (n2)
b) Find min(treeA), max(treeA), min(treeB), max(treeB) {O(n1 + n2) Worst case)}
c) The root of the merged tree, should be such that if n1 and n2 values were to be arranged in asc order, then root will be at index (n1+n2)/2 (To ensure it is balanced)
Using the info in step 1 and 2, we can find which tree this newRoot will lie in , and we can also find the node.

4) Delete this node from the tree. (O(log n)

5) Now traverse treeA in post order, delete the node from treeA, by setting the node's left and right pointers to null. Add it in the tree with the root as newRoot.
(Pls note that we are not creating any new node, but only changing pointers

6) do the same with treeB

Complexity: O((n1 + n2) log(n1 + n2))
worst case (O (n1 + n2)^2)

Constant space

- P December 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

As pointed out in the question, n1 and n2 are not needed to be equal. they can as well be different

- P December 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

1)Serialize both trees
2)Merge sort
3)Convert Merged Doubly Linked List to a Balanced BST

- Rohan March 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Don't we need to just insert the root of tree A in tree B?

- my June 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

The best you can do is to traverse both the trees inorder and store them into arrays and then merge them.

- Anonymous June 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Convert both the trees into DLL. Perform Merge sort of both the DLLs. Convert the DLL back to Balanced Binary Search Tree (Make the centre element of the resulting DLL as root always. And repeat the same for both left & right subtrees.)

- Arpitha June 01, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Arpitha: nice approach. Each of the three steps are doable in O(n+m).

- lol June 04, 2011 | 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