Facebook Interview Question for Software Engineer / Developers






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

If we assume that tree nodes either have 0 or 2 nodes, then the tree constructed should be unique. Build the tree in recursion just like a preorder traverse.

public class Main {
   private String _str;

   class Node {
      public int index;
      public Node left, right;
   }

   public Main(String str) {
      _str = str;
   }

   private Node buildTreePreorder(int curIndex) {
      Node cur = new Node();
      cur.index = curIndex;
      if (_str.charAt(curIndex)=="N") {
         cur.left = buildTreePreorder(cur.index + 1);
         cur.right = buildTreePreorder(cur.left.index + 1);
      }
      return cur;
   }

   public static void main(String[] args) {
      Main main = new Main("NNNLLLL"); // test string
      Node root = main.buildTreePreorder(0);
   }
}

- yyl September 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This line doesn't look correct to me:

cur.right = buildTreePreorder(cur.left.index + 1);

It should be:

cur.right = buildTreePreorder(cur.index + number of nodes in the left subtree + 1);

- Vikas October 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Vikas, he did exactly what you trying to suggest. :) Take a closer look.

- Safi December 16, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

is it possible?
A
B
CD
and
A
BD
C
preorder and marks are all the same:
(A,N)(B,N)(C,L)(D,L)

- iral May 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I Agree

- Sanjay May 27, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Right its an ambigous representation .

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

what if introducing left and right extra characters (denoting null) for leaf nodes?

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

Node* formtree(char *preorder, int &num)
{
   if(preorder[num]=='\0') return NULL;
   Node *t=new Node();
   if(preorder[num++]=='L')
   {
      t->left=t->right=NULL;
   }
   else
   {
      t->left=formtree(preorder,num);
      t->right=formtree(preorder,num);
   }
   return t;
}

- XYZ May 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Would you mind to explain the idea first? It helps others to see what you are doing here. Thanks.

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

@XYZ what are you assuming abt the no of children at each node..... ???

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

its the other way to ask the same question in which given the preorder of tree & construct the tree with preorder..isn't it you may wants to look @ this & can modify if need to achieve answer

http dot shashank7s dot blogspot dot com/2011/03/special-type-of-tree-is-given-where-all dot html

correct me if anything wrong

- WgpShashank June 02, 2011 | 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