Facebook Interview Questions
- 3of 3 votes
AnswersImplement stairs(N) that (collect the solutions in a list) prints all the ways to climb up a N-step-stairs
- pk March 14, 2015 in United States
where one can either take a single step or double step.
We'll use 1 to represent a single step, and 2 to represent a double step.
stairs(3)
111
12
21| Report Duplicate | Flag | PURGE
Facebook Software Engineer - 3of 5 votes
AnswersA robot has to move in a grid which is in the form of a matrix. It can go to
- thisandthat March 09, 2015 in United States
1.) A(i,j)--> A(i+j,j) (Down)
2.) A(i,j)--> A(i,i+j) (Right)
Given it starts at (1,1) and it has to go to A(m,n), find the minimum number of STEPS it has to take to get to (m,n) and write
public static int minSteps(int m,int n)
For instance to go from (1,1) to m=3 and n=2 it has to take (1, 1) -> (1, 2) -> (3, 2) i.e. 2 steps| Report Duplicate | Flag | PURGE
Facebook Software Engineer Algorithm - 4of 4 votes
AnswersGiven an m x n matrix where each row element is sorted, but the columns do not appear in sorted order, write a function to print each matrix element in sorted order.
- seanchen11235 March 06, 2015 in United States
Example matrix:
matrix = [
[20, 40, 80],
[5, 60, 90],
[45, 50, 55]
]
Your function should print 5, 20, 40, 45, 50, 55, 60, 80, 90.
Add on: Assume that we are space-constrained such that we can only hold one row in memory at a time. Optimize your function to work under such constraints as efficiently as possible.| Report Duplicate | Flag | PURGE
Facebook Software Engineer / Developer Algorithm - 3of 3 votes
AnswersQuestion: Given a sequence of positive integers A and an integer T, return whether there is a continuous sequence of A that sums up to exactly T
- ep February 27, 2015
Example
[23, 5, 4, 7, 2, 11], 20. Return True because 7 + 2 + 11 = 20
[1, 3, 5, 23, 2], 8. Return True because 3 + 5 = 8
[1, 3, 5, 23, 2], 7 Return False because no sequence in this array adds up to 7| Report Duplicate | Flag | PURGE
Facebook Software Engineer Algorithm - 0of 0 votes
AnswersGiven a string containing letter, digit, and other characters, write a function to check palindrome for only letter and digit. The implementation need to be in-place, no extra memory is allowed to create another string or array.
- naomi.lijing@googlemail.com February 25, 2015 in UK
For example:
"ABA" is palindrome
"A!#A" is palindrome
"A man, a plan, a canal, Panama!" is palindrome| Report Duplicate | Flag | PURGE
Facebook Software Developer - 0of 0 votes
AnswersWrite a program that reverses a linked list without using more than O(1) storage.
- eng.ahmed.moustafa February 24, 2015 in United States| Report Duplicate | Flag | PURGE
Facebook - 1of 1 vote
AnswersWrite a program that answers YES/NO search queries containing * placeholders. Example: if the data you have is (hazem, ahmed, moustafa, fizo), then you should answer as follows for:
- eng.ahmed.moustafa February 24, 2015 in United States
ahmed: YES
m**stafa: YES
fizoo: NO
fizd: NO
*****: YES
****: YES
**: NO
Your program should be able to answer each search query in O(1).| Report Duplicate | Flag | PURGE
Facebook - 2of 2 votes
AnswersInput: A string equation that contains numbers, '+' and '*'
- huji February 23, 2015 in Israel
Output: Result as int.
For example:
Input: 3*5+8 (as String)
Output: 23 (as int)| Report Duplicate | Flag | PURGE
Facebook Software Engineer - 1of 1 vote
AnswersGiven a class Range
class Range { public int begin; public int end; public Range(int begin, int end) { this.begin = begin; this.end = end; } }
The problem:
- huji February 23, 2015 in Israel
Intput:
1) list of Ranges that don't overlap (not sorted)
2) newRange that might overlap.
Output:
list of merged Ranges
For example:
Input: [1..5] [9..13] [17..22]
newRange: [4..10]
Output: [1..13] [17..22]| Report Duplicate | Flag | PURGE
Facebook Software Engineer - 1of 1 vote
AnswersConvert a binary tree into a In Order traversal circular list re-purposing the node's pointers Left & Right as Previous and Next respectively.
- Nelson Perez February 22, 2015 in United States
Hint: A single node Left & Right points to itself.
Note: This is not a binary search tree.| Report Duplicate | Flag | PURGE
Facebook Software Engineer Coding - 4of 4 votes
AnswersYou are given a set of unique characters and a string.
- francisvm February 04, 2015 in United States
Find the smallest substring of the string containing all the characters in the set.
ex:
Set : [a, b, c]
String : "abbcbcba"
Result: "cba"| Report Duplicate | Flag | PURGE
Facebook Intern Algorithm - 0of 0 votes
AnswersFirst they did ask to find pattern of this
'this is a test sentence' => [t, h, i, s, i, s, a, t, e, s, t, s, e, n, t, e, n, c, e] 'thiis iss a teest seentennce' => [i, s, e, e, n] 'thiiis iss aa teeest seentennnce' => [i, e, n] 'thiiiis iss a teeest seeentennncccce' => [i, c]
after i have to do body of function
- milan.medlik January 31, 2015 in United StatesgetLongestConsecutiveChar
| Report Duplicate | Flag | PURGE
Facebook Applications Developer Algorithm - 0of 0 votes
AnswersCompletely blew it on this question today.
- jsdude January 28, 2015 in United States
1.) Given an array, find the maximum difference between two array elements given the second element comes after the first.
For example.
array = [1,2,3,4,5,6,7]
We can take the difference between 2 and 1 (2-1), but not the different between 1 and 2 (1-2).
This question is super easy, I solved it within minutes of getting of the phone. I came up with an O(n^2) solution over the phone. My improved solution was O(n).| Report Duplicate | Flag | PURGE
Facebook Software Engineer Arrays - -1of 1 vote
AnswersIt started with simple behavioral questions like, why facebook, cultural fit questions etc. They asked simple question.
You are at latest version of committed code. assume one branch of code. Code version is in sorted order. It is corrupted with bug. You have fun isBug(VerNumber) which returns True or False. Find the version in which bug was introduced?
This can be solved with linearly checking each version or modified binary search. Person asked to write test cases? This is where i struggled. how do you write test case for this question? Do you guys use framework syntax or something else?
- anonymous January 22, 2015 in India| Report Duplicate | Flag | PURGE
Facebook Senior Software Development Engineer Algorithm - 2of 4 votes
AnswersInplace reverse a sentence
You given a sentence of english words and spaces between them.
Nothing crazy:
1) no double spaces
2) no empty words
3) no spaces at the ends of a sentencevoid inplace_reverse(char* arr, int length) { // your solution }
Example:
- Sergey January 17, 2015 in United States
input "I wish you a merry Christmas"
output "Christmas merry a you wish I"
Constrains: O(1) additional memory| Report Duplicate | Flag | PURGE
Facebook Software Engineer / Developer Algorithm - 5of 5 votes
AnswersThe closest common ancestor in a tree forest.
class Node { Node* parent; // == null for root of tree Node* left; Node* right; } Node* tree_forest[]; // array of pointers which points to roots of each tree respectively Node* closest_common_ancestor(Node* n1, Node* n2) { // your solution }
Example:
| a | j | / \ | / | b c | h | / / \ | |d e f |
for e and d CCA is a
- Sergey January 17, 2015 in United States
for e and f CCA is c
for e and c CCA is c
for h and d CCA is null
Constrains: O(1) additional memory| Report Duplicate | Flag | PURGE
Facebook Software Engineer / Developer Algorithm - 0of 0 votes
AnswersDesign a URL system.
- JSDUDE January 16, 2015 in United States
He even wanted to know what kind of algorithm to use, improve the speed, availability etc.| Report Duplicate | Flag | PURGE
Facebook Software Engineer / Developer System Design - 2of 2 votes
AnswersGiven a 4 X 4 game slot that has random alphabets in all the slots
- JSDUDE January 16, 2015 in United States
Write a function that takes the keyboard and the word as input and returns true if the word can be formed
False otherwise.
A word can be formed on the board by connecting alphabets adjacent to each other (horizontal, vertical and diagonally)
Same alphabet should not be reused.| Report Duplicate | Flag | PURGE
Facebook Software Engineer / Developer Algorithm - 1of 1 vote
AnswersGiven a set of n people, find the celebrity.
- JSDUDE January 16, 2015 in United States
Celebrity is a person who:
1. Knows only himself and no one else
2. Every one else knows this person
You are given the following helper function:
bool knows(i, j);
Returns:
True: If i knows j
False: otherwise
I proposed the O(n2) algorithm at first but he wanted me to improve on it. He wanted an O(n) algorithm| Report Duplicate | Flag | PURGE
Facebook Software Engineer / Developer - 0of 0 votes
AnswersYou have a list of words with ranking.
- JSDUDE January 16, 2015 in United States
Now you need to create a function that will take this list as input and provide a way so that a T9 keyboard can provide three top results of probable words based on rankings for the numbers punched in.| Report Duplicate | Flag | PURGE
Facebook Software Engineer / Developer Algorithm - -1of 1 vote
AnswersTree to List: convert a binary tree to a circular doubly-linked list
- Kiara January 15, 2015 in United States| Report Duplicate | Flag | PURGE
Facebook Software Engineer / Developer - -1of 1 vote
Answersroll two dice, what is the probability of rolling no sixes?
- Kiara January 12, 2015 in United States| Report Duplicate | Flag | PURGE
Facebook Software Engineer / Developer - 2of 2 votes
AnswersGiven an array of integers, return true if there're 3 numbers adding up to zero (repetitions are allowed)
- fatenuller January 09, 2015 in United States
{10, -2, -1, 3} -> true
{10, -2, 1} -> true -2 + 1 +1 =0| Report Duplicate | Flag | PURGE
Facebook Software Engineer Intern Algorithm - 1of 1 vote
AnswersFind the maximum number of non-intersecting events in a calendar.
- Kiara January 09, 2015 in United States for Infrastructure| Report Duplicate | Flag | PURGE
Facebook Software Engineer / Developer - 0of 0 votes
AnswersWrite a function to print the rows of a binary tree, terminating each row with a carriage return
- Kiara January 09, 2015 in United States| Report Duplicate | Flag | PURGE
Facebook Software Engineer / Developer - 1of 1 vote
AnswersGiven a Tree:
A /\ B C /\ /\ D E F G
Write a function that prints:
- santidltp January 05, 2015 in United States
A
BC
DEFG| Report Duplicate | Flag | PURGE
Facebook Software Engineer / Developer Algorithm - 6of 6 votes
AnswersGiven an array of ages (integers) sorted lowest to highest, output the number of occurrences for each age.
- streamer December 31, 2014 in United States
For instance:
[8,8,8,9,9,11,15,16,16,16]
should output something like:
8: 3
9: 2
11: 1
15: 1
16: 3
This should be done in less than O(n).| Report Duplicate | Flag | PURGE
Facebook Software Engineer / Developer Algorithm - 2of 2 votes
AnswersPrint a BST such that it looks like a tree (with new lines and indentation, the way we see it in algorithms books).
- Ray December 22, 2014 in United States| Report Duplicate | Flag | PURGE
Facebook Software Engineer Intern Algorithm - 2of 2 votes
AnswersWrite a function that takes the following inputs and gives the following outputs.
- forfron December 19, 2014 in United States
Input: A list of points in 2-dimensional space, and an integer k
Output: The k input points closest to (5, 5), using Euclidean distance
Example:
Input: {(-2, -4), (0, 0), (10, 15), (5, 6), (7, 8), (-10, -30)}, k = 2
Output: {(5, 6), (7, 8)}| Report Duplicate | Flag | PURGE
Facebook Software Engineer Intern Algorithm - 2of 2 votes
AnswersAn efficient way to sort patient files in an array of just 3 types 'High-importance', 'Mid-importance', 'Low-importance' which are in an arbitrary order (unsorted).
- Ipalibo December 19, 2014 in United States
The output preference should start with the highest.
1. High-importance
2. Mid-importance
3. Low-importance
[high,low,low,med,high,low]
ps I was told to take advantage of the fact that they are just only 3 types.| Report Duplicate | Flag | PURGE
Facebook Software Engineer / Developer Sorting