Facebook Interview Questions
- 18of 18 votes
AnswersIf a=1, b=2, c=3,....z=26. Given a string, find all possible codes that string can generate. Give a count as well as print the strings.
- diffuser78 June 07, 2013 in United States
For example:
Input: "1123". You need to general all valid alphabet codes from this string.
Output List
aabc //a = 1, a = 1, b = 2, c = 3
kbc // since k is 11, b = 2, c= 3
alc // a = 1, l = 12, c = 3
aaw // a= 1, a =1, w= 23
kw // k = 11, w = 23| Report Duplicate | Flag | PURGE
Facebook Software Engineer / Developer Algorithm Coding - 33of 35 votes
AnswersA k-palindrome is a string which transforms into a palindrome on removing at most k characters.
- sc.shobhit August 28, 2013 in India
Given a string S, and an interger K, print "YES" if S is a k-palindrome; otherwise print "NO".
Constraints:
S has at most 20,000 characters.
0<=k<=30
Sample Test Case#1:
Input - abxa 1
Output - YES
Sample Test Case#2:
Input - abdxa 1
Output - No| Report Duplicate | Flag | PURGE
Facebook Intern Algorithm - 1of 1 vote
AnswersPush all the zero's of a given array to the end of the array. In place only. Ex 1,2,0,4,0,0,8 becomes 1,2,4,8,0,0,0
- CheckThisResume.com March 09, 2012 in United States| Report Duplicate | Flag | PURGE
Facebook Software Engineer / Developer Algorithm Arrays C Coding - 4of 4 votes
AnswersWe have an array of objects A and an array of indexes B. Reorder objects in array A with given indexes in array B. Do not change array A's length.
example:
- u-11i24223 October 26, 2015 in United Statesvar A = [C, D, E, F, G]; var B = [3, 0, 4, 1, 2]; sort(A, B); // A is now [D, F, G, C, E];
| Report Duplicate | Flag | PURGE
Facebook Front-end Software Engineer Front End Web Development - 2of 4 votes
Answers/*
- friendlytechs June 25, 2014 in United States
Write an algorithm that brings all nonzero elements to the left of the array, and returns the number of nonzero elements.
Example input: [ 1, 0, 2, 0, 0, 3, 4 ]
Example output: 4
[1, 4, 2, 3, 0, 0, 0]
* The algorithm should operate in place, i.e. shouldn't create a new array.
* The order of nonzero elements does not matter
*/| Report Duplicate | Flag | PURGE
Facebook Android test engineer - 2of 2 votes
AnswersMcDonald’s sells Chicken McNuggets in packages of 6, 9 or 20 McNuggets. Thus, it is possible, for example, to buy exactly 15 McNuggets (with one package of 6 and a second package of 9), but it is not possible to buy exactly 16 McNuggets, since no non- negative integer combination of 6's, 9's and 20's add up to 16. To determine if it is possible to buy exactly n McNuggets, one has to find non-negative integer values of a, b, and c such that
- nishakothari62 November 03, 2012 in United States
6a+9b+20c=n
Write a function, called McNuggets that takes one argument, n, and returns True if it is possible to buy a combination of 6, 9 and 20 pack units such that the total number of McNuggets equals n, and otherwise returns False. Hint: use a guess and check approach.| Report Duplicate | Flag | PURGE
Facebook Developer Program Engineer Python - 0of 0 votes
AnswersString Reduction
- sf engineer February 20, 2012 in United States
Given a string consisting of a,b and c's, we can perform the following operation: Take any two adjacent distinct characters and replace it with the third character. For example, if 'a' and 'c' are adjacent, they can replaced with 'b'. What is the smallest string which can result by applying this operation repeatedly?
Input:
The first line contains the number of test cases T. T test cases follow. Each case contains the string you start with.
Output:
Output T lines, one for each test case containing the smallest length of the resultant string after applying the operations optimally.
Constraints:
1 <= T <= 100
The string will have at most 100 characters.
Sample Input:
3
cab
bcab
ccccc
Sample Output:
2
1
5
Explanation:
For the first case, you can either get cab -> cc or cab -> bb, resulting in a string of length 2.
For the second case, one optimal solution is: bcab -> aab -> ac -> b. No more operations can be applied and the resultant string has length 1.
For the third case, no operations can be performed and so the answer is 5.| Report Duplicate | Flag | PURGE
Facebook Amazon Software Engineer / Developer Algorithm - 2of 4 votes
AnswersGiven a sequence of numbers A(1) ..A(n), find the continuous subsequenceA(i)..A(j) for which the sum of elements is maximum.
- pirate July 28, 2013 in India
condition: we should not select two contiguous numbers| Report Duplicate | Flag | PURGE
Yahoo Facebook Software Engineer / Developer Algorithm - 7of 15 votes
AnswersWAP to modify the array such that arr[I] = arr[arr[I]].
- praveen February 28, 2014 in United States
Do this in place i.e. with out using additional memory.
example : if a = {2,3,1,0}
o/p = a = {1,0,3,2}
Note : The array contains 0 to n-1 integers.| Report Duplicate | Flag | PURGE
Facebook Software Engineer / Developer Algorithm - 6of 6 votes
AnswersCode a function that receives a string composed by words separated by spaces and returns a string where words appear in the same order but than the original string, but every word is inverted.
Example, for this input string@"the boy ran"
the output would be
@"eht yob nar"
Tell the complexity of the solution.
- diegum June 06, 2014 in United States for iOS| Report Duplicate | Flag | PURGE
Facebook Software Engineer / Developer String Manipulation - 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 - 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 - 1of 3 votes
AnswersGiven a random string S and another string T with unique elements, find the minimum consecutive sub-string of S such that it contains all the elements in T.
- Saghar H November 05, 2015 in United States
example:
S='adobecodebanc'
T='abc'
answer='banc'| Report Duplicate | Flag | PURGE
Facebook Software Developer Algorithm - 3of 5 votes
AnswersYou're given an array of integers(eg [3,4,7,1,2,9,8]) Find the index of values that satisfy A+B = C + D, where A,B,C & D are integers values in the array.
- omair.ahmed08 October 09, 2014 in United States
Eg: Given [3,4,7,1,2,9,8] array
The following
3+7 = 1+ 9 satisfies A+B=C+D
so print (0,2,3,5)| Report Duplicate | Flag | PURGE
Facebook Software Engineer / Developer Algorithm Arrays Data Structures - 1of 1 vote
AnswersGiven an array of positive integers (excluding zero) and a target number. Detect whether there is a set of consecutive elements in the array that add up to the target.
- m.mirzamo October 28, 2015 in United States
Example: a = {1, 3, 5, 7, 9}
target = 8
output = true ({3, 5})
or target = 15
output = true : {3, 5, 8}
but if target = 6, output would be false. since 1 and 5 are not next to each other.| Report Duplicate | Flag | PURGE
Facebook Intern Algorithm - 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 - 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 - 6of 6 votes
AnswersWrite a function for retrieving the total number of substring palindromes.
- Andrew November 04, 2013 in United States
For example the input is 'abba' then the possible palindromes= a, b, b, a, bb, abba
So the result is 6.
Updated at 11/11/2013:
After the interview I got know that the O(n^3) solution is not enough to go to the next round. It would have been better to know before starting implementing the solution unnecessarily ...| Report Duplicate | Flag | PURGE
Facebook Software Engineer / Developer Algorithm - 1of 1 vote
AnswersGiven an array, remove the duplicates and return a unique array keeping the first occurrence of the duplicates and the order.
- kchronis October 29, 2013 in United States for iOS
[@2, @1, @3, @1, @2] --> [@2, @1, @3]| Report Duplicate | Flag | PURGE
Facebook iOS Developer Arrays - 1of 1 vote
Answersthere is a pyramid with 1 cup at level , 2 at level 2 , 3 at level 3 and so on..
- anonymous July 14, 2011 in United States
It looks something like this
1
2 3
4 5 6
every cup has capacity C. you pour L liters of water from top . when cup 1 gets filled , it overflows to cup 2,3 equally, and when they get filled , Cup 4 and 6 get water only from 2 and 3 resp but 5 gets water from both the cups and so on.
Now given C and M .Find the amount of water in ith cup.| Report Duplicate | Flag | PURGE
Facebook Software Engineer / Developer Dynamic Programming - 4of 4 votes
AnswersGiven an unordered array of positive integers, create an algorithm that makes sure no group of integers of size bigger than M have the same integers.
- Fred_Castro January 22, 2014 in United States
Input: 2,1,1,1,3,4,4,4,5 M = 2
Output: 2,1,1,3,1,4,4,5,4| Report Duplicate | Flag | PURGE
Facebook Software Engineer / Developer Algorithm - 0of 0 votes
Answersthree points are randomly chosen on a circle.what the probability that
- David August 02, 2011
1.triangle formed is right angled triangle.
2.triangle formed is acute angled triangle.
3.triangle formed is obtuse angled triangle.| Report Duplicate | Flag | PURGE
Facebook Software Engineer / Developer Math & Computation - 2of 2 votes
AnswersGiven a list of arrays of time intervals, write a function that calculates the total amount of time covered by the intervals.
- aonecoding4 January 19, 2019 in United States
For example:
input = [(1,4), (2,3)]
return 3
input = [(4,6), (1,2)]
return 3
input = {{1,4}, {6,8}, {2,4}, {7,9}, {10, 15}}
return 11| Report Duplicate | Flag | PURGE
Facebook Software Engineer - 5of 5 votes
AnswersGiven "n", generate all valid parenthesis strings of length "2n".
- anusha136 December 02, 2013 in United States
Example:
Given n=2
Output:
(())
()()| Report Duplicate | Flag | PURGE
Facebook Intern Algorithm - 5of 5 votes
AnswersGiven two strings, return boolean True/False, if they are only one edit apart.Edit can be insert/delete/update of only one character in the string. Eg:
- abkr990 October 31, 2014 in United States
-True
xyz,xz
xyz, xyk
xy, xyz
-False
xyz, xyz
xyz,xzy
x, xyz| Report Duplicate | Flag | PURGE
Facebook Software Engineer / Developer Algorithm - 5of 5 votes
AnswersLet's say you have 10,000 servers, each with a billion integers. How do you find the median?
- 352905 July 03, 2014 in United States| Report Duplicate | Flag | PURGE
Facebook Software Engineer / Developer Algorithm - 0of 0 votes
AnswersGiven an array write a function to print all continuous subsequences in the array which sum of 0.
- Mukesh August 13, 2013 in United States
e.g:
Input:
Array = [-1, -3, 4, 5, 4]
output:
-1, -3, 4| Report Duplicate | Flag | PURGE
Facebook Algorithm - 2of 2 votes
AnswersGiven an virtual 4x4 boggle board, and some 4 letter words, determine if the words are in the board
- rosie March 22, 2013 in United States
ex.
S M E F
R A T D
L O N I
K A F B
STAR- no
TONE- no
NOTE- yes
SAND- yes
etc.| Report Duplicate | Flag | PURGE
Facebook Software Engineer Intern - 0of 0 votes
AnswersGiven two input arrays, return true if the words array is sorted according to the ordering array
- releb February 28, 2018 in United States
Input:
words = ['cc', 'cb', 'bb', 'ac']
ordering = ['c', 'b', 'a']
Output: True
Input:
words = ['cc', 'cb', 'bb', 'ac']
ordering = ['b', 'c', 'a']
Output: False| Report Duplicate | Flag | PURGE
Facebook Software Engineer - 11of 13 votes
Answersinput [2,3,1,4]
- balahana March 07, 2014 in United States
output [12,8,24,6]
Multiply all fields except it's own position.
Restrictions:
1. no use of division
2. complexity in O(n)| Report Duplicate | Flag | PURGE
Facebook Software Engineer / Developer