Facebook Interview Question for Solutions Engineers


Country: Europe
Interview Type: Phone Interview




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

public class SinglyLinkedListBackward {






	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		LinkedNode root = new LinkedNode("A");
		root.next =  new LinkedNode("B");
		root.next.next = new LinkedNode("C");
		
		//recursiveReverse(root);
		//iterativeReverse(root);
		iterativeReverseWithOutMemory(root);

	}

	
	/**
	 * reverse linked list using iterative approach with O(n2) runtime
	 * reverse the LinkedList and iterate
	 * @param root
	 */
	private static void iterativeReverseWithOutMemory(LinkedNode root) {
		if(root == null) return;
		LinkedNode previousNode = null;
		LinkedNode currentNode = root;
		LinkedNode nextNode = null;
		
		
		while(currentNode.next != null){
			
			nextNode = currentNode.next;
			currentNode.next = previousNode;
			previousNode = currentNode;
			currentNode = nextNode;
			
		}
		currentNode.next = previousNode;
		
		while(currentNode != null){
			System.out.print(currentNode.value+" -> ");
			currentNode = currentNode.next;
		}
	}

	/**
	 * reverse linked list using iterative approach
	 * push node to stack, and pop it
	 * consume O(n) memeory
	 * @param root
	 */
	private static void iterativeReverse(LinkedNode root) {
		if(root == null) return;
		Stack<LinkedNode> stack = new Stack<>();
		
		while(root!=null){
			stack.push(root);
			root = root.next;
		}
		
		while(!stack.isEmpty()){
			System.out.print(stack.pop().value+ "->");
		}
		
	}

	/**
	 * reverse linked list using recursion
	 * @param root
	 */
	private static void recursiveReverse(LinkedNode root) {
		if(root == null) return;
		recursiveReverse(root.next);
		System.out.print(root.value+ "->");
	}
	
	
	
	
	
	
}

class LinkedNode{
	public LinkedNode next;
	public String value;
	public LinkedNode(String value){
		this.value = value;
	}

}

- Goutham March 16, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

#1) Recursively call on linked list and when recursive operation returns print the current character - recursive also takes O(n) memory as n recursive calls
#2) Iterate over the linked list and add the characters to arraylist and then print them or string reverse after adding them to a string

public class Solution {
  // Question 3
  public void printCharBackward(Node node) {
    if(node == null) {
      return;
    }
    int length = getLinkedListLength(node);
    for(int i = length - 1; i >= 0; i++) {
      int index = 0;
      Node tmp = node;
      while(index != i) {
        tmp = tmp.next;
        index++;
      }
      System.out.printLn(tmp.data);
    }
  }

  private int getLinkedListLength(Node node) {
    int length = 0;
    Node tmp = node;
    while(tmp != null) {
      length++;
      tmp = tmp.next;
    }
    return length;
  }

  ///////////////// Question 4
  public void printCharacterIterative(Node node) {
    Node prev = null;
    Node tmp = node;
    // Reverse the list in (N)
    while(tmp != null) {
      Node next = tmp.next;
      tmp.next = prev;
      prev = tmp;
      tmp = next;
    }
    // Print it O(N)
   tmp = prev;
    while(tmp != null) {
      System.out.printLn(tmp.data);
      tmp = tmp.next;
    }
  }
}

- nk March 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Console.WriteLine("reverLinkedlist");
            LinkedList<string> list = new LinkedList<string>();
            list.AddLast("A");
            list.AddLast("B");
            list.AddLast("C");

            foreach (var item in list)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine("print result");

            foreach(var item in printbackward(list))
            {
                Console.WriteLine(item);
            }
            

            Console.ReadLine();


LinkedList<string> printbackward(LinkedList<string> list1)
            {
                LinkedList<string> list2 = new LinkedList<string>();
                foreach(var item in list1)
                {
                    list2.AddFirst(item);
                }

                return list2;
            }

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

1. recursion - iterate through list and print after recursion call.
2. Iterative with O(n) memory - put into stack and print poping from the stack.
3. Iterative with O(1) memory and O(n^2) time - for each element starting from the last till the first, iterate to its location and print it.
4. Iterative with O(1) memory and O(n) time with data structure change - iterate over the list and for every element keep the previous one and the next one. Change the current element to point to the previous one instead of the next one (null for the first element). At the end the linked list will be reversed and you can just iterate over it.

- Y2 May 03, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For #4 we can reverse the linked list in place:

Node *Reverse(Node *head)
{
	Node *prev = NULL;
	Node *n = head;
	while (n) {
		Node *next = n->next_;
		n->next_ = prev;
		prev = n;
		n = next;
	}
	return prev;
}

- Alex May 17, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Recursive

def recursivly(l):
    if l is None:
        return l
    recursivly(l.next)
    print(l.data)

Iterate

def iterate(l):
    temp = []
    while l is not None:
        temp.append(l.data)
        l = l.next
    while len(temp) >= 1:
        print(temp.pop())

n

def n(head):
    if head is None:
        return head
    current = head
    previous = None
    while current is not None:
        next = current.next
        current.next = previous
        previous = current
        current = next
    return previous

n^2

def n2(head):
    temp = head
    n = 0
    while temp is not None:
        n += 1
        temp = temp.next
    for i in range(0, n):
        x = head
        for k in range(0, n-i-1):
            x = x.next
        print(x.data)

- Emin May 21, 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