Google Interview Question for Software Engineer / Developers






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

Simple approach is:

insert element in tree:
whenever tree is unbalanced do left or right rotation: this is exactly AVL tree prob

- Rahul D May 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Node *BalancedBST(int A[], int start, int end) {
if(start< end) {
int mid = (start + mid)/2;
Node *temp = (Node *) malloc(sizeof(Node));
temp->val = A[mid];
temp->left = BalancedBST(start, mid-1);
temp->right = BalancedBST(mid+1, end);
return temp;
}
else return NULL;
}

- Sriram April 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What a lame solution!

@Sriram: before replying READ the question first. The input is NOT NECESSARILY in sorted order that you can use "balanced BST from sorted array" algorithm here.

My opinion is that AVL/Red-black tree is best solution to go. However, it's too challenging to code it absolutely correctly in an interview.

- anonymous April 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@anonymous
It is not exactly lame.. before commenting, THINK first..

1. For the given input string, create a simple binary search tree
2. Insert the element into the tree at using the usual methods
3. Read the created binary search tree in inorder format such that the generated list/array you have is sorted
4. Then perform the above given BalancedBST to rebalance the tree

- Jane April 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

insert_node(node *root, int key)
{
if (key > root->key) {
if (has_one_child(root)) {
if (root->right) {
int temp=root->key
int temp1 = successor(root->key);
delete(successor(root->key));
root->key= temp1;
root->left=(node *)malloc(sizeof(node));
root->left->key=temp;
insert_node(root,key)
}
} else {
insert_node (root->right, key);
}
} else {
if (has_one_child(root)) {
if (root->left) {
int temp=root->key
int temp1 = predecessor(root->key);
delete(predecessor(root->key));
root->key= temp1;
root->right=(node *)malloc(sizeof(node));
root->right->key=temp;
insert_node(root,key)
} else {
insert_node(root->left, key);
}
}
}

- Ankit April 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int buildBinaryTree(node *current, int data) {
if (current == NULL ) {
current = newNode(data);
current->left = NULL;
current->right = NULL;
return 0;
}

if (current->data >= data) {
buildBinaryTree(current->left, data);
else if (current->data < data) {
builBinaryTree(current->right, data);
}
}

int main (){
node * temp = NULL;
for(int i =0; i< n; ++i) {
buildBinaryTree(temp,arry[i]);
}
return 0;
}

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

I think this is wht happens in Red Black Tree, Same concept of having balanced tree with property of BST.

- Kaustubh May 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The interviewer is asked for implementation of self-balance tree. Most self-balance tree implementation are complex and hard to remember. So far, left-lean red black tree[1] is the easiest and most memorable implementation I've ever seen.

ref1: Left-Leaning. Red-Black Trees. Robert Sedgewick. Princeton University.

case class Node(value: Int,
           isRed: Boolean = true,
           left: Option[Node] = None,
           right:Option[Node] = None) {


  def flipColors: Node = {
    this.copy(isRed = !this.isRed,
      left = left.map(l => l.copy(isRed = !l.isRed)),
      right = right.map(r => r.copy(isRed = !r.isRed))
    )
  }

  def rotateLeft: Node = {
    val left = this.copy(right = this.right.flatMap(_.left), isRed = true)
    val ret = this.right.get.copy(left = Some(left), isRed = this.isRed)
    return ret
  }

  def rotateRight: Node = {
    val right = this.copy(left = this.left.flatMap(_.right), isRed = true)
    val ret = this.left.get.copy(right = Some(right), isRed = this.isRed)
    return ret
  }
}

case class LLRB(root: Option[Node] = None) {

  def insert(value: Int): LLRB = {
    return new LLRB(Some(insert(root, value)))
  }

  private def insert(head: Option[Node], value: Int): Node = {
    head match {
      case None => new Node(value)
      case Some(v) => {
        var h = v
        if (isRed(h.left) && isRed(h.right)) {
          h = h.flipColors
        }

        if (h.value < value) {
          h = h.copy(right = Some(insert(h.right, value)))
        } else {
          h = h.copy(left = Some(insert(h.left, value)))
        }

        if (isRed(h.right)) {
          h = h.rotateLeft
        }
        if (isRed(h.left) && isRed(h.left.flatMap(_.left))) {
          h = h.rotateRight
        }
        h
      }
    }
  }

  private def isRed(node: Option[Node]): Boolean = {
    return node.map(_.isRed).getOrElse(false)
  }
}

- yunglin May 16, 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