Facebook Interview Question


Country: Israel




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

Assume we have access to the root and the tree is complete.

private boolean fixTree(Node node) {
	if (node == null) {
		return false;
	}
	if (node.left == null || node.right == null) {
		// The node is a leaf, no need to fix
		return node.value;
	}
	node.value = fixTree(node.left) && fixTree(node.right);
	return node.value;
}

- Jason Saruulo September 19, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Here is the solution I gave to the interviewer:

#include "CommonHeader.h"

struct TreeNode {
    TreeNode* parent;
    TreeNode* left;
    TreeNode* right;
    bool value;
    TreeNode() : parent(NULL), left(NULL), right(NULL) {}
    /**
     * @brief return true if this node value was actually fixed. False otherwise
     * @return 
     */
    bool updateValue() {
        bool oldValue = this->value;
        this->value = this->left && this->right; // same as LOGICAL AND
        return oldValue != this->value;
    }
};

void fixTree(TreeNode* node) {
    node->value = !node->value;
    
    TreeNode* cur = node->parent;
    while(cur) {
        if(!cur->updateValue())
            break;
        cur = cur->parent;
    }
}

- PenChief September 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

@PenChief: I think that's the best solution, as well with stopping when value doesn't toggle anymore. I was surprised he accepted the parent pointer in the Node. How ever it's easy enough to fix that.

- Chris September 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Tree * fixTree(Tree* root){
	if (root == NULL || (root->left == NULL && root->right == NULL))
		return root;
 	Tree *l_val=fixTree(root->left);
	Tree *r_val=fixTree(root->right);
	
	if(root->data != (l_val->data && r_val->data))
		root->data = !root->data;
	return root;
}

- ssr September 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Please tell me if there is any bugs.

private void fixTree(TreeNode root, TreeNode node)
  {
    fixTreeUtil(root, node);
  }
  
  private boolean fixTreeUtil(TreeNode root, TreeNode node)
  {
    if(root == null) return false;
    if(root == node) return true;
    boolean cl = fixTreeUtil(root.left, node);
    boolean cr = fixTreeUtil(root.right, node);
    if(cl || cr)
    {
      root.val = root.left.val && root.right.val;
      return true;
    }
    else return false;
  }

class TreeNode {
        boolean val;
        TreeNode left;
        TreeNode right;
    }

- Ankit.jainb27 October 01, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@PenChief

Small correction here:
this->value = this->left && this->right;
This line will set value = 1 as long as we have valid left and right child. We actually need to read the value of left and right child in order to set the value of parent to Logical AND.

Please correct me if I'm wrong here.

- mr.robot.fun.society November 01, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is similar to bubble-up operation in heaps.

- sri November 26, 2017 | 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