Facebook Interview Question for Software Engineer / Developers






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

#define MAX(x,y) ((x > y) ? x:y)
int height(struct node *root)
{

if(root == null)
return 0;
ldepth = height(root->left) + 1;
rdepth = height(root->right) + 1;

return MAX(ldepth, rdepth);
}

- Assange December 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this is wrong soln as it goes only left-left-left and rht-rht-rht
i ve posted the right soln...

- hi December 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It's a right one. Each level of recursion traverses all the nodes.

- sophia July 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

int maxDepth(struct node * node){
	if(node==NULL)
		return(0);
	else {
		int ldepth=maxDepth(node->left);
		int rdepth=maxDepth(node->right);
		if(ldepth>rdepth)
			return(ldepth+1);
		else
			return(rdepth+1);
	}
}

- 47 February 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

but where is the usage of binary search tree property
this is like the solution for binary tree

- Anonymous December 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why don't you tell us?

- Anonymous December 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Iterative Approach. Recursion may be killing perf

// FindDepthOfABinaryTree.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
//using namespace stdadv;
class Node{
public:
int info;
Node* left;
Node* right;

Node(int info,Node* left, Node* right)
{
this->info = info;
this->left = left;
this->right = right;
}
};
Node* getTree()
{
Node * Root= new Node(5,new Node(6,new Node(8,NULL,new Node(10,NULL,NULL)),new Node(9,NULL,NULL)),new Node(7,new Node(8,NULL,NULL),new Node(9,NULL,NULL)));
return Root;
}

int getDepth(Node* root)
{
queue<Node*> q;
q.push(root);
int depth=-1;
Node * marker = root;

if(root== NULL)
{
return -1;
}else{
while(q.size()!=0)
{
//cout<<q.size();
/*The below is imp to note pop will not return value but front will*/
Node* gotNodeFromQueue= (Node*)q.front();
q.pop();

if(gotNodeFromQueue != root || depth == -1)
{
if(gotNodeFromQueue->left != NULL)
{
q.push(gotNodeFromQueue->left);
}
if(gotNodeFromQueue->right != NULL)
{
q.push(gotNodeFromQueue->right);
}
}

if(gotNodeFromQueue == root )
{

// We reached a marker and one level complete
depth++;
if(q.size() != 0)
q.push(root);
}
}
}

return depth;
}

int main()
{
Node * root = getTree();

int depth=getDepth(root);

cout<< "Depth:" << depth;
}

- Anonymous December 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

In this problem, we are not required to use the searching property of the BST (left <= right), since we are not concerned about that when finding the depth. So, then it remains a binary tree only.

- Assange December 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int btree::minheight(btreenode *k,queue q1,queue q2)
{
int h = -1;
q1.enqueue(k);
while(q1.front != -1)
{
btreenode* temp;
while(q1.front != -1)
q2.enqueue(q1.dequeue());
h++;
while(q2.front != -1)
{
temp = q2.dequeue();
if(temp -> leftchild != NULL)
{
q1.enqueue(temp -> leftchild);
}
else
{
cout << "Min height = " << h << endl;
system("pause");
exit(0);
}
if(temp -> rightchild != NULL)
{
q1.enqueue(temp -> rightchild);
}
else
{
cout << "Min height = " << h << endl;
system("pause");
exit(0);
}
}
}
return h;
}

- hi December 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Recursive approach:

int depthTree(Tree t) {
if(t==null || t.left==null || t.right==null)

}

- Dan January 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ignore this.

- Dan January 02, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Recursive approach:

int depthTree(Tree t) {
if(t==null || t.left==null || t.right==null)
return 0;
return(MAX(1+depthTree(t.left) , 1+depthTree(t.right));
}

- Dan January 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can take advantage of BST by using heuristic DFS with pruning

- Jan January 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Clue: create two string of preorder and inorder and apply some logic

- PKT February 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can you utilize the sorted BST behavior?

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

public int maxDepth(Node root)
{
if (root == null) { return 0; }

return 1 + Math.Max(maxDepth(root.leftNode), maxDepth(root.rightNode));
}

- anup.h.nair December 31, 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