## Facebook Interview Questions

- 0of 0 votes
`class Log{ String fun_name; String enterOrExit;// enter or exit int time; public Log(String fun_name, String enterOrExit, int time){ this. fun_name = fun_name; this. enterOrExit = enterOrExit; this.time = time; } } public void printInclusiveAndExclusiveTime (List<Log> logs){ } Log for some functions, calculate the inclusive time and exclusive time for each function E.g Fun_name enter / exit time F1 enter 1 F2 enter 2 F2 exit 3 F1 exit 4 F1: inclusive time = 4-1 = 3, exclusive time = 3-1 = 2 F2: inclusive time = 3-2 = 1, exclusive time = 1`

- 0of 0 votes
Find the subarray within an array (containing at least TWO number) which has the largest sum.

For example, given the array [-2,-1,-3,-4,-1],

the contiguous subarray [-2,-1] has the largest sum = -3.

try to do it in O(n) time

Followup, if input is stream, how to solve it

public int maxSubArray(int[] nums) {}

- 2of 2 votes
Facebook Senior Engineer On-site 2017

1st Round

Question 1: Binary tree to doubly linked list.

Question 2: Read 4 (Given the read4 API, read an entire file)

2nd Round

Culture fit. No coding.

3rd Round

Question: System Design POI (Point of Interest. Given a point, find things within a radius).

Lunch

4th Round

Question 1: Decode way

Question 2: Random max index

5th Round

Question: System design + component-wise design on download manager

- 0of 0 votes
Group by with having related questions. ER provided was customer table.

- 0of 0 votes
Find the % of all male customers in a specific area out of all the customers in that area.

- 0of 0 votes
Get total number of all the departments of each employees

- 0of 0 votes
A "derangement" of a sequence is a permutation where no element appears in its original position. For example ECABD is a derangement of ABCDE, given a string, may contain duplicate char, please out put all the derangement

public List<char[]> getDerangement(char[]){}

- -2of 2 votes
three sum with duplicate, pirnt all indexes, for example:

0 2 -2 -2

(0)(1)(2)(3)

print (0, 1, 2) (0, 1, 3)

can you do it use n^2 (or less) time complexity with as less space as possible?

public List<List<Integer>> threeSum(int[] nums) {}

- 0of 0 votes
Given an array of integers:

1. rearrange the array such that all non-zero members appear on the left of the array (order is not important)

2. return the number of non-zero members

e.g. [1,2,0,5,3,0,4,0] => [1,2,5,3,4,0,0,0] and return 5. The non-zero array members can be in any order.

- 0of 2 votes
Given an array of task and k wait time for which a repeated task

needs to wait k time to execute again. please rearrange the task

sequences to minimize the total time to finish all the tasks.

Example

Tasks = 111222, k = 2,

One possible task sequence is

12_12_12,

another possible task sequence is 21_21_21

thus you shoud return 8

public int getMiniTime(int[] nums, int k){

}

follow up, output one of the sequence 12_12_12, or 21_21_21

- 0of 0 votes
Given an unsorted array, sort it in such a way that the first

element is the largest value, the second element is the smallest,

the third element is the second largest element and so on.

[2, 4, 3, 5, 1] -> [5, 1, 4, 2, 3]

can you do it without using extra space

public void sortAlternate(int[] nums){}

- 0of 0 votes
Given a number of tasks (T) and servers (S), find out if the tasks can be accommodated on the servers. Each Task has a number of Units and each server has a number of Slots on which Units can run.

The only condition is that two Units of the same Task "cannot" run on the same Server.

Servers

S[0] = "SS1", "SS2", "SS3", SS4 //Slots // 4 -> 3 -> 2 -> 1

S[1] = "SS1", "SS2" // 2 -> 1 -> 0 -> false

S[2] = "SS1", "SS2", SS3, SS4, SS5 // 5 -> 4 -> 3 -> 2

S[3] = "SS1", "SS2", SS3, SS4, SS5 // 5 -> 4 -> 3 -> 2

Example:

S[0] = 4

S[1] = 3

S[2] = 5

S[3] = 5

...

Tasks

T[0] = U0, U1, U2, U3 //Tasks

T[1] = U0, U1

T[2] = U0, U1, U2

...

Example:

T[0] = 4

T[1] = 2

T[2] = 3

implement

boolean boolean CanRunTasks(S[], T[]){

}

- 0of 0 votes
`class UndirectedGraphNode { int label; List<UndirectedGraphNode> neighbors; UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<>(); } } public boolean isBipartite(List<UndirectedGraphNode> nodes){ }`

use DFS algorithm to check the bipartite-ness of a graph

- 1of 1 vote
Given some email ids, and a similarity function which says whether two email ids are similar, determine all the sets of email ids that are similar to each other.

- 0of 0 votes
You have a string consisting of open and closed parentheses, but parentheses may be imbalanced.

Make the parentheses balanced and return the new string.

- 0of 0 votes
Design a system which takes in latitude and longitude and returns back closest 5 locations.

- 1of 1 vote
Given an array of integers greater than zero, find if it is possible to split it in two (without reordering the elements), such that the sum of the two resulting arrays is the same. Print the resulting arrays.

- 3of 3 votes
there is a bunch of tasks, each have different time to complete, task is independent, and then there are some workers,

How to allocate tasks to these workers to minimize the total time to complete all the task. The tasks can be randomly picked from the task list.

Example

Task: 2,2,3,7, 1

Worker: 2.

Return 8, because the first worker can work on the first three tasks : 2 + 2 + 3 = 7, and the second worker can work on the last two tasks : 7 + 1 = 8, so the total time to finish all the task is 8.

public int getMini(int[] tasks, int k)

- 0of 0 votes
We have a List of FlightRoute

`public static class FlightRoute { String from; String to; int time; .... }`

and write a function to find Shortest Path: findShorestPath(String start, String end, List<FlightRoute>routes)

- 0of 0 votes
Iterate over a singly linked list backwards. Call print on each node.

Example: The list A->B->C should print as

"C B A"`class Node { public Node next; public String value; }`

There are 4 solutions

1) recursive

2) iterative with O(n) memory

3) iterative with O(1) memory and O(n²) runtime

4) iterative with O(1) memory and O(n) runtime (for this solution the initial list may be modified)

Explain all 4 solutions and write the code for solutions 3 and 4

- 0of 0 votes
You are given an array of integers.

Write an algorithm that brings all nonzero elements to the left of the array, and returns the number of nonzero elements.

The algorithm should operate in place, i.e. shouldn't create a new array.

The order of the nonzero elements does not matter. The numbers that remain in the right portion of the array can be anything.

Example:

given the array [ 1, 0, 2, 0, 0, 3, 4 ],

a possible answer is [ 4, 1, 3, 2, ?, ?, ? ], 4 non-zero elements, where "?" can be any number.

Code should have good complexity and minimize the number of writes to the array.

- 1of 1 vote
Given:

a encoded to 1

b encoded to 2

....

z encoded to 26

You can translate a number to a string:

'123' can be translated to 'abc'

but also can be translated to 'aw','lc' which gives 3 total translations

'12' can be translated to 'ab' and 'l' -> 2 translations

Write a function to get the number of valid combinations from a number like '123123123'

- 0of 0 votes
You have a string of numbers, i.e. 123. You can insert a + or - sign in front of ever number, or you can leave it empty. Find all of the different possibilities, make the calculation and return the sum.

For example;

+1+2+3 = 6

+12+3 = 15

+123 = 123

+1+23 = 24

...

-1-2-3 = 6

...

Return the sum of all the results.

- 0of 0 votes
Design a algorithm to initialize the board of Candy Crush Saga. With M x N board, Q types of candies. (Rules: no 3 for run after initialization, must contain at least one valid move at the beginning)

- 0of 0 votes
Given the newest 100 entries of a person's facebook newsfeed. How would you rank the entries. The (for the user) most important ones should be ranked first. Which features would you use and how do you train/improve your model (machine learning)?

- 1of 9 votes
Q: Weighted meeting room

Given a series of meetings, how to schedule them. Cannot attend more than a meeting at the same time. Goal is to find maximum weight subset of mutually non-overlap meetings.`class Meeting: def __init__(self): self.startTime self.endTime self.weight`

@concernedCoder

When you claim the questions as fake, provide evidence. These are no doubt questions asked in the coding interviews of the best companies and they definitely help interviewees to prepare for the interview.

Why do you have a problem with this?

- 2of 2 votes
given preorder traversal [5,3,2,4,8,7,9] of a BST, how do we identify the leaf nodes without building the tree ?

@Anonymous Thanks for the reply!

Please try other use cases like, only single leaf node

- 2of 2 votes
Given estimated stock quotes, in an array, print the maximum profit from a buy and sell. i.e [19, 22, 15, 35, 40, 10, 20] would show a profit of 25(40 -15). The sale must come after the buy. Solve this in O(N) time.

`<?php function print_max_profit($arr){ if(count($arr) < 1) return 0; $len = sizeof($arr); $profit = 0; $least = $arr[0]; for($i = 0; $i < $len; $i++){ $least = min($least,$arr[$i]); $profit = max($profit, $arr[$i] - $least); } return $profit; }`

- 1of 1 vote
Given a set of possibly overlapping rectangles in different levels (All of which are "not rotated", can be uniformly represented as (left-bottom,right-top) tuplets), return a minimal set of (non-rotated) non-overlapping rectangles, that occupy the same area.

The rectangle at lower level has more priority than at higher levels.

- 1of 1 vote
I got my interview yesterday and the problem they asked me was: Giving a method intToEnglish that receives an int as a parameter, how do you return its representation in english words. The number can be of any size but no more than around 2 billion since the parameter is an int 2ˆ32