## Facebook Interview Question for Software Engineers

• 3

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.left = res;  //make linked list circular
res.right = res;
return res;  //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.right = root;
root.left = prev;
res = prev;
}
if(next != null) {
next.left = root;
root.right = next;
res = next;
}
return res;
}``````

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);
}
}``````

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

4.1 is a good one.

``````data Tree a = Leaf a | Bin (Tree a) (Tree a)

data Circular a = Node a (Circular a) (Circular a)

-- pre-order traversal
--
--   .
--  / \
-- 0   .
--    / \
--   .   .
--  / \  |\
-- 1   5 7 3
--
-- 0 <-> 1 <-> 5 <-> 7 <-> 3 <-> 0 <-> ...
--
toCircular :: Tree a -> Circular a
toCircular t =
let
go :: Tree a -> Circular a -> Circular a
go (Leaf a) n =
let
x =
Node a x n
in
x
go (Bin t1 t2) n =
let
x1 =
go t1 x2
x2 =
go t2 n
in
x1
in
go t (toCircular t)

-- view 8 . toCircular \$ t
-- [0,1,5,7,3,0,1,5]
view :: Int -> Circular a -> [a]
view 0 _ =
[]
view n (Node a _ t2) =
a : view (n-1) t2``````

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.

### 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.