Facebook Interview Question for Software Engineers


Country: United States
Interview Type: Phone Interview




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

void flatten(Node head) {
	if (head == null) {
		return;
	}
	System.out.println(head.char);
	flatten(head.down);
	flatten(head.right);
}

- dorashine September 14, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 4 vote

flatten (Node head) {
	if (head == null) return;
	
	print(head.a);
	
	Node n = head;
	while (n.down != null) {
		n = n.down;
		print(n.a);
	}
	
	flatten(head.right);
}

- corbindlee July 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

public void flatten(Node head){

	if(head == null){
		return;
	}
	if(head.right ==null && head.down == null){
             System.out.print(head.a);
             return;
        }
	Node current = null;
	Node down = null;

	while(head != null){
		
		current = head;
		down = head.down;
		head = head.right;
		//This is just to print pretty,	
		System.out.print(current.a+"->");
		
		while(down != null){
                        if(down.down == null && head == null)
                            System.out.print(down.a)
                        else
			    System.out.print(down.a+"->");
			down = down.down;
		}	
	}
}

- HiAnd July 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

public void flatten(Node head) {
        Node tmpParentRight = head.right;

        head.right = null;
        while (head.down != null) {
            head.right = head.down;
            head.down = null;
            head = head.right;
        }
        head.right = tmpParentRight;
        
        if(tmpParentRight != null)
            flatten(tmpParentRight);
    }

- Raj July 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In-place solution.

// Definition for singly-linked list.
struct ListNode {
    char val;
    ListNode *next, *down;
    ListNode(char x) : val(x), next(NULL), down(NULL) {}
};
 
class Solution {
public:
    
    ListNode* flatten(ListNode* head) {
        ListNode* current = head;
        while( current  ){
        	ListNode* tmp = current -> next;
        	while( current -> down ){
        		current -> next = current -> down;
        		current -> down = NULL;
        		current = current -> next;
        	}
        	current -> next = tmp;
        	current = current -> next;
        }
        return head;
    }
};

- jariasf03 July 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public Node flatten(Node head)
{
	if (head==null||head.next==null && head.down==null)
	{
		return head;
	}
	
	Node v=head;
	while(v!=null)
	{
		Node tmp=v.next;
		Node s=v;
		Node f=s.down;
		while(f!=null)
		{
			s.next=f;
			s.down=null;
			s=f;
			f=f.down;
		}
		s.next=tmp;
		v=s.next;
	}
	return head;
}

- divm01986 July 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

typedef struct _MNode
{
	struct _MNode* right;
	struct _MNode* down;
	char a;
} MNode;

void flattenOneNode(MNode*& head)
{
	MNode* tmphead = head;
	while (tmphead->down != nullptr)
	{
		tmphead->right = tmphead->down;
		tmphead = tmphead->down;
	}
	head = tmphead;
}

void flatten(MNode*& head)
{
	MNode* tmphead = head;
	MNode* next = nullptr;

	while (tmphead != nullptr)
	{
		next = tmphead->right;
		flattenOneNode(tmphead);
		tmphead->right = next;
		tmphead = next;
	}
}

- LANorth July 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void flatten(Node head){
Node temp=null;
while(head!=null)
    	  {
    		  temp=head;
    		  System.out.print(temp.getData()+" ");
    		  while(temp.getDown()!=null)
    		  {
    			temp=temp.getDown();
    			System.out.print(temp.getData()+" ");
    		  }
    		  temp=head.getRight();
    		  head=head.getRight();
    
    	  }
}

- Anonymous July 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void flatten(Node head)
{
Node temp=null;
while(head!=null)
    	  {
    		  temp=head;
    		  System.out.print(temp.getData()+" ");
    		  while(temp.getDown()!=null)
    		  {
    			temp=temp.getDown();
    			System.out.print(temp.getData()+" ");
    		  }
    		  temp=head.getRight();
    		  head=head.getRight();
    
    	  }
}

- itsneerupriyanitkkr July 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It's not just printing output, you will need to change the pointers also

- Raj July 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void flatten(Node head){
Node cur = head;
Node temp;
while(cur != null){
temp = cur.right;
Node down = cur.down;
while(down != null){
cur.right = down;
cur.down = null;
cur = down;
down = down.down;
}
cur.right = temp;
cur = temp;
}
}

- ultikhopdi July 16, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void flatten(Node* head)
{
    if (nullptr != head)
    {
        cout << head->a << "->";
        flatten(head->down);
        flatten(head->right);
    }
}

- enricomax July 16, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void flatten(Node* head)
{
    Node* hNode = head;
    while (nullptr != hNode)
    {
        Node* vNode = hNode;
        while (nullptr != vNode)
        {
            if (vNode != head)
            {
                std::cout << " -> ";
            }
            
            std::cout << vNode->a;
            vNode = vNode->down;
        }
        
        hNode = hNode->right;
    }
}

- GeraldG July 20, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public Node flatten(Node head){
		if(head==null){
			return null;
		}
		Node headNew=head;
		Node nodeRight=head.getRight();
		while(head!=null){
			Node nodeDown=head.getDown();
			if(nodeDown!=null){
				head.setRight(nodeDown);
				head=nodeDown;
			}else{
				head.setRight(nodeRight);
				head=nodeRight;
				if(nodeRight.right!=null){
					nodeRight=nodeRight.right;
				}else{
					nodeRight=null;
					head=null;
				}
			}
		}
		return headNew;

}

- np July 23, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public Node flatten(Node head){
		if(head==null){
			return null;
		}
		Node headNew=head;
		Node nodeRight=head.getRight();
		while(head!=null){
			Node nodeDown=head.getDown();
			if(nodeDown!=null){
				head.setRight(nodeDown);
				head=nodeDown;
			}else{
				head.setRight(nodeRight);
				head=nodeRight;
				if(nodeRight.right!=null){
					nodeRight=nodeRight.right;
				}else{
					nodeRight=null;
					head=null;
				}
			}
		}
		return headNew;
	}

- np July 23, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public Node flatten(Node head){
if(head==null){
return null;
}
Node headNew=head;
Node nodeRight=head.getRight();
while(head!=null){
Node nodeDown=head.getDown();
if(nodeDown!=null){
head.setRight(nodeDown);
head=nodeDown;
}else{
head.setRight(nodeRight);
head=nodeRight;
if(nodeRight.right!=null){
nodeRight=nodeRight.right;
}else{
nodeRight=null;
head=null;
}
}
}
return headNew;
}

- np July 23, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public Node flatten(Node head){
		if(head==null){
			return null;
		}
		Node headNew=head;
		Node nodeRight=head.getRight();
		while(head!=null){
			Node nodeDown=head.getDown();
			if(nodeDown!=null){
				head.setRight(nodeDown);
				head=nodeDown;
			}else{
				head.setRight(nodeRight);
				head=nodeRight;
				if(nodeRight.right!=null){
					nodeRight=nodeRight.right;
				}else{
					nodeRight=null;
					head=null;
				}
			}
		}
		return headNew;
	}

- np July 25, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Flatten(Node Head)
{
while(Head != null)
{
Node RightNode = Head.Right;
while(Head != null)
{
Print(Head.a);
Head = Head.Down;
}
Head = RightNode;
}
}

- Natan Colenberg July 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Flatten(Node Head)
{
	while(Head != null)
	{
		Node RightNode = Head.Right;
		while(Head != null)
		{
			Print(Head.a);
			Head = Head.Down;
		}
		Head = RightHead;
	}
}

- natan.colenberg July 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

function flattenNode($head) {
if($head) {
if($head->next == NULL && $head->down == NULL) {
return $head;
}
while($head) {
$temp = $head->next;
while($head->down) {
$head->next = $head->down;
$head->down = NULL;
$head = $head->next;
}
$head->next = $temp;
$head = $head->next;
}
}
return $head;


}

- Anonymous July 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Node {
	public $next,$down,$data;

	public function __construct($data) {
		$this->data = $data;
	}



}

function flattenNode($head) {
		if($head) {
			if($head->next == NULL && $head->down == NULL) {
				return $head;
			}
			while($head) {
				$temp = $head->next;
				while($head->down) {
					$head->next = $head->down;
					$head->down = NULL;
					$head = $head->next;
				}
				$head->next = $temp;
				$head = $head->next;
			}
		}
		return $head;
		

}

- Rahul July 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Node {
	public $next,$down,$data;

	public function __construct($data) {
		$this->data = $data;
	}



}

function flattenNode($head) {
		if($head) {
			if($head->next == NULL && $head->down == NULL) {
				return $head;
			}
			while($head) {
				$temp = $head->next;
				while($head->down) {
					$head->next = $head->down;
					$head->down = NULL;
					$head = $head->next;
				}
				$head->next = $temp;
				$head = $head->next;
			}
		}
		return $head;
		

}

- Rahul July 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Node {
	public $next,$down,$data;

	public function __construct($data) {
		$this->data = $data;
	}



}

function flattenNode($head) {
		if($head) {
			if($head->next == NULL && $head->down == NULL) {
				return $head;
			}
			while($head) {
				$temp = $head->next;
				while($head->down) {
					$head->next = $head->down;
					$head->down = NULL;
					$head = $head->next;
				}
				$head->next = $temp;
				$head = $head->next;
			}
		}
		return $head;
		

}

- rahul July 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void printDown(const Node& H)
{
        cout<<H.a<<"->";
        if(H.down)
                printDown(*(H.down));
}

void flatten(const Node& H)
{
        cout<<H.a<<"->";
        if(H.down)
                printDown(*(H.down));

        if(H.right)
                flatten(*(H.right));
}

- aret July 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

# python
# assume only roots have right pointers
def flatten(n, sb=""):
  if n is None:
    return 
  right = n.right
  print n.c,
  if right: print "->",
  while n.down:
    n = n.down
    print n.c + "->",
  flatten(right, sb)

- Spencer Aiello July 30, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class FlattenNode {

	static class Node {
		Node right;
		Node down;
		char a;
	}
	
	public static void main(String[] args) {
		
		Node E = new Node();
		E.a = 'E';
		
		Node L = new Node();
		L.a = 'L';
		
		Node D = new Node();
		D.a = 'D';
		D.down = L;
		D.right = E;
		
		Node Y = new Node();
		Y.a = 'Y';
		
		Node T = new Node();
		T.a = 'T';
		T.down = Y;
		T.right = E;
		
		Node Z = new Node();
		Z.a = 'Z';
		
		Node X = new Node();
		X.a = 'X';
		X.down = Z;
		
		Node N = new Node();
		N.a = 'N';
		N.down = X;
		N.right = T;
		
		Node A = new Node();
		A.a = 'A';
		
		Node C = new Node();
		C.a = 'C';
		C.down = A;
		
		Node M = new Node();
		M.a = 'M';
		M.down = C;
		M.right = N;
		
		flatten(M);
		
	}
	
	static void flatten(Node head) {
		if(head == null)
			return;

		System.out.print(head.a + "->");
		
		flatten(head.down);
		flatten(head.right);
	}

}

- bosung90 August 07, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def flatten(head):
	if not head:
		return
	node = head
	while node:
		next_node = node.right
		while node.down:
			tmp = node.down
			node.right = tmp
			node = tmp
		node.right = next_node
		node = next_node
	return head

- beyourwish August 17, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def flatten(head):
	if not head:
		return
	node = head
	while node:
		next_node = node.right
		while node.down:
			tmp = node.down
			node.right = tmp
			node = tmp
		node.right = next_node
		node = next_node
	return head

- beyourwish August 17, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

next_node = node.right

- beyourwish August 17, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why not just:

class Node{
public:
	void flatten();
	Node *right;
	Node *down;
	char a;
};

void Node::flatten(){
	for ( Node *r = this; r; r = r->right ){
		cout << r->a << "  ";
		for ( Node *d = r->down; d; d = d->down ){
			cout << d->a << "  ";
		}
	}
	cout << endl;
}

this approach assumes:
1. There is only 1 way to each node from the root
2. Nodes do not point to theirselves
3. Nodes below the first level do not have neighbours to the right

- darkshine August 18, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

func flatten(node: Node?) {
    guard let node = node else { return }
    print("\(node.val) ", terminator: "")
    
    flatten(node: node.down)
    flatten(node: node.right)
}

- tomkowz October 05, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

func flatten(node: Node?) {
    guard let node = node else { return }
    print("\(node.val) ", terminator: "")
    
    flatten(node: node.down)
    flatten(node: node.right)
}

- tomkowz October 05, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void Flatten(Node head)
{
	var list = new List<>();
 	var p = head;
	while (p != null)
	{
		list.Add(p.A);
		var d = p.Down;
		while (d != null)
		{
			list.Add(d.A);
			d = d.Down;
		}
		
		p = p.Right;
	}
	
	return list;
}

- hnatsu October 19, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Recursive:

public void flatten(Node head){
	if(head != null){
		System.out.println(head.a);
		flatten(head.down);
		flatten(head.right);
}
}

Iterative:

public void flatten(Node head){
	Stack<Node> stack = new Stack<>();
	stack.push(head);

	while(!stack.isEmpty()){
		head = stack.pop();

		while(head != null){
			System.out.println(head.a);
			if(head.right != null)
				stack.push(head.right);
			head = head.down;
		}
}
}

- libertythecoder October 24, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def flatten(head):
    if not head: return head
    result = ''
    while head:
        down_pointer = head
        while down_pointer:
            result += down_pointer.val+'->'
            down_pointer = down_pointer.down
        head = head.right
    return result[:-2]

- Kailash November 14, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def flatten(head):
    if not head: return head
    result = ''
    while head:
        down_pointer = head
        while down_pointer:
            result += down_pointer.val+'->'
            down_pointer = down_pointer.down
        head = head.right
    return result[:-2]

- kailash November 14, 2016 | 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