## Facebook Interview Question for Android Engineers

• 1
of 1 vote

Country: United States
Interview Type: Phone Interview

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

For example :
Input : 123456789
Output : One hundred twenty three million four hundred fifty six thousand seven hundred eighty nine

Logic :

After thousand number , there is a symmetry in special names like million,billion,trillion,quadrillion,quintillion.
The symmetry is if you multiply 1000 with thousand than we will get a million. If we multiply 1000 with million then we will get billion. If we multiply 1000 with billion then we get trillion.Similarly,1000 multiply trillion equals quadrillion. 1000 multiply quadrillion equals quintillion.This symmetry starts after we reach 1000 number . So we will divide the program into two parts .

First part that is function convertLessThanOneThousand(int number) will convert any number smaller than 1000 into words.
Second part, starting from extreme-right of input number, we will use modulus operator by1000, to get the last three extreme right digits of the input number.Taking three digits at a time from right to left , we will scan the whole input number until it is fully converted into the word.

Java Code for writing Numbers in Words :

``````public class NumberToWord

{
private static final String[] specialNames = {
"",
" thousand",
" million",
" billion",
" trillion",
" quintillion"
};

private static final String[] tensNames = {
"",
" ten",
" twenty",
" thirty",
" forty",
" fifty",
" sixty",
" seventy",
" eighty",
" ninety"
};

private static final String[] numNames = {
"",
" one",
" two",
" three",
" four",
" five",
" six",
" seven",
" eight",
" nine",
" ten",
" eleven",
" twelve",
" thirteen",
" fourteen",
" fifteen",
" sixteen",
" seventeen",
" eighteen",
" nineteen"
};

private String convertLessThanOneThousand(int number) {
String current;

if (number % 100 < 20){
current = numNames[number % 100];
number /= 100;
}
else {
current = numNames[number % 10];
number /= 10;

current = tensNames[number % 10] + current;
number /= 10;
}
if (number == 0) return current;
return numNames[number] + " hundred" + current;
}

public String convert(int number) {

if (number == 0) { return "zero"; }

String prefix = "";

if (number < 0) {
number = -number;
prefix = "negative";
}

String current = "";
int place = 0;

do {
int n = number % 1000;
if (n != 0){
String s = convertLessThanOneThousand(n);
current = s + specialNames[place] + current;
}
place++;
number /= 1000;
} while (number > 0);

return (prefix + current).trim();
}

public static void main(String[] args) {
NumberToWord obj = new NumberToWord();
System.out.println("*** " + obj.convert(123456789));
System.out.println("*** " + obj.convert(-55));
}
}``````

Result:

one hundred twenty three million four hundred fifty six thousand seven hundred
eighty nine

negative fifty five

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

see here question?id=5765581773996032

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

Broke the number into 3 digit chunks and concatenated all of them

``````public class IntToEnglish {
private String[] ones = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
private String[] teens = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
private String[] tens = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
private String[] thousands = {"", "thousand", "million", "billion"};

private String evaluateChunk(int n) {
int hundredsPlace = n/100 % 10;
int tensPlace = n/10 % 10;
int onesPlace = n % 10;
String thousandsChunk = "";
if (hundredsPlace != 0) {
thousandsChunk += ones[hundredsPlace] + " " + "hundred";
if (tensPlace != 0 || onesPlace != 0)
thousandsChunk += " ";
}
if (tensPlace == 1)
thousandsChunk += teens[onesPlace];
else if (tensPlace > 1 && onesPlace == 0)
thousandsChunk += tens[tensPlace];
else if (tensPlace > 1 && onesPlace != 0)
thousandsChunk += tens[tensPlace] + " " + ones[onesPlace];
else
thousandsChunk += ones[onesPlace];
return thousandsChunk;
}

public String evaluate(int n) {
if (n == 0) return "zero";
String englishNum = "";
String negativeString = "";
int thousandsPlace = 0;
if (n < 0) {
negativeString = "negative";
n *= -1;
}
while (n != 0) {
int chunk = n % 1000;
if (chunk != 0)
englishNum = evaluateChunk(n%1000) + " " + thousands[thousandsPlace] + " " + englishNum;
thousandsPlace++;
n /= 1000;
}
return negativeString + englishNum;
}``````

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

``````var ones = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"];
var teens = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"];
var tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"];
var thousands = ["", "thousand", "million", "billion"];

function ten(number) {
var ret = "";
if(number < 10) {
ret = ones[number];
} else if (number < 20) {
ret = teens[number - 10];
} else if (number < 100) {
ret = tens[parseInt(number / 10)] + ' ' + ones[number % 10];
}

return ret;
}

function hundred(number) {
var ret = "";
if(number > 100) {
ret += ten(parseInt(number / 100)) + ' hundred ';
}

ret += ten(number % 100);
return ret;
}

function intToEnglish(number) {
if(number == 0) return 'zero';

var ret = 0 > number ? "negative " : "";
number = Math.abs(number);

function chunk(size, name) {
if(number >= size) {
ret += hundred(parseInt(number / size)) + ' ' + name + ' ';
number %= size;
}
};

chunk(1000000000, 'billion');
chunk(1000000, 'million');
chunk(1000, 'tousand');
chunk(100, 'hundred');
ret += hundred(number);

return ret.trim();``````

}

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

``````var ones = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"];
var teens = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"];
var tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"];

function ten(number) {
var ret = "";
if(number < 10) {
ret = ones[number];
} else if (number < 20) {
ret = teens[number - 10];
} else if (number < 100) {
ret = tens[parseInt(number / 10)] + ' ' + ones[number % 10];
}

return ret;
}

function hundred(number) {
var ret = "";
if(number > 100) {
ret += ten(parseInt(number / 100)) + ' hundred ';
}

ret += ten(number % 100);
return ret;
}

function intToEnglish(number) {
if(number == 0) return 'zero';

var ret = 0 > number ? "negative " : "";
number = Math.abs(number);

function chunk(size, name) {
if(number >= size) {
ret += hundred(parseInt(number / size)) + ' ' + name + ' ';
number %= size;
}
};

chunk(1000000000, 'billion');
chunk(1000000, 'million');
chunk(1000, 'tousand');
chunk(100, 'hundred');
ret += hundred(number);

return ret.trim();
}``````

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

``````#Dictionaries for reference
ones = {"1":"One","2":"Two","3":"Three","4":"Four","5":"Five","6":"Six", "7":"Seven","8":"Eight","9":"Nine"}
afterones = {"10":"Ten","11":"Eleven","12":"Twelve","13":"Thirteen","14":"Fourteen","15":"Fifteen","16":"Sixteen", "17":"Seventeen","18":"Eighteen","19":"Nineteen"}
tens = {"2":"Twenty","3":"Thirty","4":"Fourty","5":"Fifty","6":"Sixty", "7":"Seventy","8":"Eighty","9":"Ninety"}
grand={0:" Billion, ",1:" Million, ",2:" Thousand, ",3:""}

#Function converting number to words of 3 digit
def num_to_wrds(val):
if val != "000":
ans = ""
if val in ones:
x = val
ans = ans + ones[val] + " Hundered and "
if val[1:] in afterones:
ans = ans + afterones[val[1:]] + " "
elif val in tens:
ans = ans + tens[val] + " "
if val in ones:
ans = ans + ones[val]
return ans

num = input("Enter the number: ")

final =""
numlis = [numinwrds[0:3],numinwrds[3:6],numinwrds[6:9],numinwrds[9:12]]

for key,grp in enumerate(numlis):

if grp!="000":
final = final + num_to_wrds(grp) + grand[key]

print final``````

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

I doubt if this kind of questions are asked in Facebook. There is not much algo required in this. may be fake.

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

Study CTCI.

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.