Facebook Interview Question for Software Engineer / Developers






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

void print_tree(const node * root)
{
    list<const node*> pending_nodes;

    pending_nodes.push_back(root);
    while ( !pending_nodes.empty() )
    {
        const node* cur = pending_nodes.front();
        pending_nodes.pop_front();

        cout << cur->key << endl;
        if ( cur->left ) pending_nodes.push_back(cur->left);
        if ( cur->right ) pending_nodes.push_back(cur->right);
    }
}

- dumbcoder November 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

it need not be a binary tree (with max of 2 children), it's an arbitrary tree.
First, define the data structure used to represent the tree - adjacency list?

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

BFS should do the job, right?

- AW November 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

But how would you know when to hit a new line?
Eg
1
2 3 4
5 6 7 8 9
I suppose this is per level printing..
You need to know the structure of the tree..or can use 2 queues and keep switching between them...add children to second queue while iterating over first one till its empty...then switch first and second queue and again perform...pretty poor space complexity but time will be theta(n).

- dxcrazy November 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

only need one queue...
You can use two counters and switching between them. nbCurrentLevel-- when pop and print one node
nbNextLevel++ when push one node
When nbCurrentLevel == 0 print a new line...

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

Is it a binary tree?
How is the structure of the tree defined if it isn't

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

every node stores two references: first child and next sibling.

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

do one rthing do more

- azad November 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Create two Queues A, B.
2. Start from Root.
3. Put all childrens into A.
4. Do a While A is not Empty. Print the Node, put all thier children into QueueB.
5. Now copy B into A. Repeat 4.
6. if A is empty and B is empty. Quit.

- @A November 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Don't understand why we need two queues, one is sufficient. Steps should be like,
1. push root to the queue.
2. if queue is NOT empty
pop the element from the queue.
print it.
if the element has any children,
push all its children to the queue.

Repeat step 2 until queue is empty.
Any comments?

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

If we need to print all nodes in a single line,
e.g. 1 2 3 4 5 6 7 8
both of above are correct, so better to go with only one Queue.
But to print nodes as dxcrazy said:
1
2 3 4
5 6 7 8
Above won't work.

- Anurag Singh January 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Added null after all nodes in one level are added in queue, as an indicator of one level traversal
int min_depth(BT t)
{
Queue q;
Enqueue(Q,t);
Enqueue(q,null);
while(!IsEmptyQ(q))
{
node=Dequeue(q);
if(q==null)
{
printf("\n");
if(!IsEmptyQ(q))
Enqueue(q,null);
}
else
{
printf("%d ",node->data);
Enqueue all childs of node in q;
}
}
}

- Anurag Singh January 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static class Node {
        public Node(String value) {
            this.value = value;
        }

        public String value;
        public List<Node> children = new ArrayList<>();
    }

    public static void printTreeByLevels(Node root){
        List<Node> rootLevel = new ArrayList<>();
        rootLevel.add(root);
        printLevel(rootLevel);
    }

    public static void printLevel(List<Node> level){
        List<Node> nextLevel = new ArrayList<>();
        for (Node n : level){
            System.out.printf("%s ", n.value);
            nextLevel.addAll(n.children);
        }
        if (!nextLevel.isEmpty()) {
            System.out.println("\n");
            printLevel(nextLevel);
        }
    }

- prosto.mail April 28, 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