Facebook Interview Question for Software Developers


Team: Infrastructure
Country: United States
Interview Type: In-Person




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

Solution without recursion, O(n) time and O(1) space

public static Node reverse(Node node) {
        if (node == null)
            return null;

        Node next = node.next;
        node.next = null;

        while(next != null) {
            Node temp = next.next;
            next.next = node;
            node = next;
            next = temp;
        }
        return node;
    }

- kredible July 13, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void reverseList(struct node * root){
	
	if(root->next==NULL){
		head=root;
		return;
	}
	reverseList(root->next);
	struct node * q=root->next;
		q->next=root;
		root->next=NULL;
	return;
}
int main(){
	
	reverseList(head);

}

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

void reverseList(struct node * root){
	
	if(root->next==NULL){
		head=root;
		return;
	}
	reverseList(root->next);
	struct node * q=root->next;
		q->next=root;
		root->next=NULL;
	return;
}
int main(){
	reverseList(head);
}

- bhuvanachandu9 July 08, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

First solution is using stack:

public ListNode reverseWithStack(ListNode listNode) {
        Stack<Integer> stack = new Stack<>();
        while (listNode != null) {
            stack.push(listNode.val);
            listNode = listNode.next;
        }
        ListNode reversed = null;
        ListNode result = null;
        while (!stack.isEmpty()) {
            ListNode current = new ListNode(stack.pop());
            if (reversed == null) {
                reversed = current;
                result = reversed;
            } else {
                reversed.next = current;
                reversed = reversed.next;
            }
        }
        return result;
    }

The second solution doesn't use stack:

public ListNode reverseWithoutStack(ListNode listNode) {
        ListNode result = null;
        while (listNode != null) {
            ListNode curr = new ListNode(listNode.val);
            if (result == null) {
                result = curr;
            } else {
                curr.next = result;
                result = curr;
            }
            listNode = listNode.next;
        }
        return result;
    }

- niki July 11, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Solution using recursion and iteration both:

class Main {
    static  Node head;
    public static void main(String[] args) {
        Node root = new Node(1);
        Node n1 = new Node(2);
        Node n2 = new Node(3);
        Node n3 = new Node(4);
        Node n4 = new Node(5);
        Node n5 = new Node(6);
        root.next=n1;n1.next=n2;n2.next=n3;n3.next=n4;n4.next=n5;
        printll(root);
        head=root;

        //iterative
        Node currnode = root,prevnode=null,nextnode=null;
        while(currnode!=null){
            nextnode=currnode.next;
            currnode.next=prevnode;
            prevnode=currnode;
            currnode=nextnode;
            head =prevnode;

        }
        printll(head);

        //recursive
        reverse(head);
        printll(head);

    }
    public static void printll(Node root){
        System.out.println("");
        while(root!=null){
            System.out.print(" "+root.data);
            root=root.next;
        }
    }

    public static void reverse(Node root){
        if(root.next==null) {
            head=root;
            return;}
        reverse(root.next);

        Node n = root.next;
        n.next=root;
        root.next=null;
    }
}

class Node{
    int data;
    Node next;

    public Node(int d){
        data=d;
    }
}

- krupen.ghetiya July 27, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

foldl (flip (:)) []

- Anonymous December 05, 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