Facebook Interview Question for Android Engineers


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",
        " quadrillion",
        " 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

- aquinarumao February 09, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

see here question?id=5765581773996032

- Chris February 08, 2017 | Flag Reply
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;
    }

- JasonK February 08, 2017 | Flag Reply
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();

}

- Max February 09, 2017 | Flag Reply
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();
}

- Max February 09, 2017 | Flag Reply
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[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

- Bharath Kumar February 18, 2017 | Flag Reply
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.

- sandeep.bvb March 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Study CTCI.

- JohnK July 07, 2017 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More