Facebook Interview Question for Software Engineers


Country: United States
Interview Type: In-Person




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

Looking for interview experience sharing and coaching?

Visit aonecode.com for private lessons by FB, Google and Uber engineers

ONE TO ONE real-time coaching offers

SYSTEM DESIGN Courses (highly recommended for candidates for FLAG & U)
ALGORITHMS (conquer DP, Greedy, Graph, Advanced Algos & Clean Coding),
latest interview questions sorted by companies,
mock interviews.

Our students got hired from G, U, FB, Amazon, LinkedIn and other top tier companies after weeks of training.

Feel free to email us aonecoding@gmail.com with any questions. Thanks!

solution 4.1

public TreeNode solve(TreeNode root) {
        TreeNode[] res = treetoDLL(root);
        if(res == null) return null;
        res[0].left = res[1];  //make linked list circular
        res[1].right = res[0];
        return res[0];  //return a node in the chain
    }

    public TreeNode[] treetoDLL(TreeNode root) { //recursion to create linked list in place    

        if(root == null) return null;
    

        TreeNode[] prev = treetoDLL(root.left);

        TreeNode[] next = treetoDLL(root.right);


        TreeNode[] res = new TreeNode[] {root, root};
    

        if(prev != null) {

            prev[1].right = root;

            root.left = prev[1];

            res[0] = prev[0];

        }
        
if(next != null) {

            next[0].left = root;

            root.right = next[0];

            res[1] = next[1];

        }

        return res;

    }

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

Let say
Small array size = m
Large array size = n

Steps: Loop through each element of small array(O(m)) and do binary search on large array O(log(n)) = O(mlog(n))

public class InterSectionOfSortedArrays {
	
	//O(mlog(n))
	public static void getIntersection(int[] arr1 , int[] arr2){
		
		for(int i=0;i<=arr2.length-1;i++){ //O(m)
			if(findInSmallArray(arr1,arr2[i])){ // O(log(n))
				System.out.println(arr2[i]);
			}
		}
	}
	
	//O(log(n)) Binary Search
	public static boolean findInSmallArray(int[] arr , int value){ // serachArray, valueTobeSearched 
		int low=0;
		int high=arr.length-1;
		
		while(low<=high){
			int mid = (low+high)/2;
			if(arr[mid] == value){
				return true;
			}else if(arr[mid] < value){
				low=mid+1;
			}else{
				high=mid-1;
			}
		}
		return false;
	}
	
	
	public static void main(String[] args) {
	
		int [] arr1 = {1, 3, 4, 5, 7,8,9,10,11}; // very large
		int [] arr2 = {2, 3, 5, 6, 9, 10}; 	// small
		getIntersection(arr1,arr2);
	}
}

- Mayank Jain August 06, 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