## Facebook Interview Question

Android Engineers**Country:**United States

**Interview Type:**Phone Interview

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;
}
```

```
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();
```

}

```
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();
}
```

```
#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[0] in ones:
x = val
ans = ans + ones[val[0]] + " Hundered and "
if val[1:] in afterones:
ans = ans + afterones[val[1:]] + " "
elif val[1] in tens:
ans = ans + tens[val[1]] + " "
if val[2] in ones:
ans = ans + ones[val[2]]
return ans
num = input("Enter the number: ")
# Paddiing with zeros
pad = 12-len(str(num))
numinwrds = "0"*pad + str(num)
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
```

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 :

Result:

- aquinarumao February 09, 2017one hundred twenty three million four hundred fifty six thousand seven hundred

eighty nine

negative fifty five