Facebook Interview Question
Software EngineersCountry: United States
Interview Type: In-Person
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);
}
}
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
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
- aonecoding July 15, 2017