Google Interview Question for Software Engineer / Developers






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

Pseudo Code:

/* Bottom Up recursive code.
* Basic Idea: Swap the right child to its parent bottom up to make it a Max Heap.
*/

NODE* BSTMaxHeapify(NODE* Tree)
{
if(Tree==NULL)
return Tree;
BSTMaxHeapify(Tree->left);
BSTMaxHeapify(Tree->right);
return(BSTMaxHeapSwap(Tree,Tree->right));
}

NODE* BSTMaxHeapSwap(NODE* Root, NODE* RootRight)
{
if(RootRight==NULL)
return Root;
else
Swap the Root and RootRight Node;
return RootRight;
}

Plz comment if I have made any mistake

- gradStudent October 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

you dont have to swap the nodes, swap the data. That would be neat.

- dumdum January 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yo, in your code isn't the root node of the tree getting swapped only once with the right and going one level down instead of going all the way down to the bottom most level possible on it's right? If yes, then your code is incorrect.

- bp February 02, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I also feel this code is incorrect, it is just one bubble sort run after which you have maximum in the root. However for the root children, it is not max heap anymore.

- mbriskar June 01, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

How about just doing a simple in-order traversal of the BST. It would produce a sorted array which is indeed a max-heap!

- amshali January 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

We also need to inverse the array at the end! O(n)

- amshali January 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Tree can be traversed in decreasing order by visiting right first and then left. So no need to reverse.

- Anonymous January 06, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

however you still need extra memory for storing the output, or do you count with some way how to not use it?

- mbriskar June 01, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

max_heapify_bst(Tree *root) 
{
    if (!root) return;
    max_heapify_bst(root->left);
    max_heapify_bst(root->right);
    sift_down(root, root->right);
}

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

My mind is fried. But this seems to be working now.

public static BSTNode BSTToMaxHeap(BSTNode root)
        {
            if (root == null)
                return null;

            root.left = BSTToMaxHeap(root.left);
            root.right = BSTToMaxHeap(root.right);
            
            //sift root down
            return SiftNodeDownInBST(root);
        }

        public static BSTNode SiftNodeDownInBST(BSTNode node)
        {
            BSTNode right = node.right;
            BSTNode nodeToReturn = right;
            BSTNode left = node.left;
            while (right != null)
            {
                node.left = right.left;
                node.right = right.right;
                right.right = SiftNodeDownInBST(node);
                right.left = left;

                right = node.right;
                left = node.left;
            }
            if (left != null)
            {
                if (left.value > node.value)
                {
                    nodeToReturn = left;
                    BSTNode l = left.left;
                    BSTNode r = left.right;
                    left.right = node.right;
                    node.left = l;
                    node.right = r;
                    left.left = SiftNodeDownInBST(node);
                }
            }
            return nodeToReturn??node;
        }

- bp February 02, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do it same as Menaheap to BST. Will that not work? Please comment.

- Tulley October 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about this approach?
O(n) to convert BST to sorted LL, and O(n) to convert sorted LL to heap

node* previous;
BST2LL(node* n)
{
..if(n==null) return;
..BST2LL(n->rightChild);
..if(previous!=null)
....previous->rightChild = n; // in the linked list, the rightChild is like "next"
..else
....root=n;
..previous=n;
..BST2LL(n->leftChild);
}

//step 1. BST to Linked List
BST2LL(root);

//step 2. LL to heap
node* i1=root;
node* i2=root->rightChild;
node* i3;
if(i2!=null) i3=i2->rightChild;

while(i1!=null)
{
..node* next = i1->right;
..i1->left = i2;
..i1->right = i3;
..i1 = next;
..if(i2!=null) i2 = i2->right; if(i2!=null) i2=i2->right;
..if(i3!=null) i3 = i3->right; if(i3!=null) i3=i3->right;
}

Please comment if i have made any mistake

- Anonymous October 18, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

In a BST, all the left nodes are correct for a max heap, so you just have to swap all the right hand nodes, and bubble the right hand nodes up as the new roots:

swapRight(node, parent)
{
  if node.left
    keepLeft(node.left)

  rightHolder = node.right
  node.right = parent

  if rightHolder
    newRoot = swapRight(node.right, node)
  else
    newRoot = node

  return newRoot
}

keepLeft(node)
{
  if node.left
    keepLeft(node.left)

  if node.right
    newRoot = swapRight(node.right, node)
  else
    newRoot = node

  return newRoot
}

root = swapRight(root, null)

- Anonymous September 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

build-heap is already o(n) operation..

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

To do it inplace : convert the bst to a sorted list :
Here is the code convert a sorted linked list to min heap:
invoke with sortedLLtoMinHeap(head,head->right);

void sortedLLtoMinHeap(node* head, node* start)
{
	if( start)
	{
		head->left = start; 
		rightchild = start->right
		start->parent = head;
	head->right = rightchild;
	if( rightchild)
		rightchild->parent = head
		SortedLLtoMinHeap(head->left, rightchild->right);
	return head;
}
}

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

This is good idea and works very good. However the only problem I see here is the heap created will not have minimal length (you call it only on the left child and the right child will be alone). This is not a complete binary tree, left branch is almost always longer

- mbriskar June 01, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

TreeNode* bstToMaxHeap(TreeNode* root)
{
    if (root == NULL) return ;
    bstToMaxHeap(root->left);
    bstToMaxHeap(root->right);
    swap(root, root->right);
    sink(root->right); 
}
void sink(TreeNode* root)
{
         while (root && root->left)
         {
                      TreeNode* swapNode = root->left;
                      if (root->right && root->right->val > root->left->val)
                                     swapNode = root->right;
                     root->val = swapNode->val;
                     root = swapNode;
         }
         
}

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

T(n) = 2*T(n/2) + O(log(n))

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

I would think that doing an inorder travesral and then creating a heap out of it should be the way to go... Traversal would be done in O(n) and so should be building a heap out of the array.

Also, the extra memory would be with the array. One question: Does having an array not actually tantamount to the implicit usage of the stack in recursive calls?

- Sagar June 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Idea: revert the right-most path
For example,

10
     5     16
   1  3  14  20

to

20
           16
       10    14
     5     
   1  3

node* bst2maxheap(node *root, node *&min) {
  if (root == nullptr) {
    min = nullptr;
    return nullptr;
  }

  node *left_heap = bst2maxheap(root->left, min);

  node *right_min = nullptr;
  node *right_heap = bst2maxheap(root->right, right_min);

  root->right = left_heap;
  root->left = nullptr;

  if (right_min) {
    right_min->left = root;
    return right_heap;
  }

  return root;
}

- zjzzjz September 22, 2015 | 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