Question d'entretien d'embauche Facebook: Given a number of size <= 999... | Glassdoor.fr

Question d'entretien d'embauche

Entretien de Software Engineer New Grad New York, NY (États-Unis)

Given a number of size <= 999 billion, return its String

representation (for 123, return "One Hundred Twenty Three," not "123").
Répondre

Réponse de l'entretien

2 réponses

1

// Can retrieve the String mapping from its numerical index
public static final String[] LESS_THAN_20 = new String[]{"", "One", ... , "Nineteen"};
public static final String[] LESS_THAN_100 = new String[]{"", "Ten", "Twenty", ... , "Ninety"};
public static final String[] OTHER = new String[]{"", "Thousand", "Million", "Billion"};
public String integerToString(int num) {
if (num == 0) return "Zero";
String res = "";
int index = 0; // Access word representations for num values &gt;= 1000
while (num != 0) {
if (num % 1000 != 0) // This was the edge case I was stuck on. Say num is a value of &gt;= 1 million such that there are no non-zero digits for the thousands place values (e.g. 1,000,001), if this conditional was not checked, the result would be "One Million Thousand One" instead of "One Million One"
res = generate(num % 1000) + OTHER[index] + " " + res;
num /= 1000; // Move to the next range of digits
index++;
}
return res;

private String generate(int num) {
if (num &lt; 20) return LESS_THAN_20[num] + " ";
else if (num &lt; 100) return LESS_THAN_100[num / 10] + " " + generate(num %10);
else return LESS_THAN_TWENTY[num / 100] + " Hundred " + generate(num % 100);
}

Candidat à l'entretien, le 5 août 2017
0

Looking for interview experience sharing and coaching?

Visit aonecode.com for private lessons by FB, Google and Uber engineers

ONE TO ONE real-time coaching on SYSTEM DESIGN, ALGORITHMS, and mock interviews.

Feel free to email us aonecoding@gmail.com with any questions. Thanks!

public class NumberToWords {
private final String[] belowTen = new String[] {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
private final String[] belowTwenty = new String[] {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
private final String[] belowHundred = new String[] {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};

public String numberToWords(int num) {
if (num == 0) return "Zero";
return helper(num);
}

private String helper(int num) {
String result = new String();
if (num &lt; 10) result = belowTen[num];
else if (num &lt; 20) result = belowTwenty[num -10];
else if (num &lt; 100) result = belowHundred[num/10] + " " + helper(num % 10);
else if (num &lt; 1000) result = helper(num/100) + " Hundred " + helper(num % 100);
else if (num &lt; 1000000) result = helper(num/1000) + " Thousand " + helper(num % 1000);
else if (num &lt; 1000000000) result = helper(num/1000000) + " Million " + helper(num % 1000000);
else result = helper(num/1000000000) + " Billion " + helper(num % 1000000000);
return result.trim();
}
}

AONECODE.COM, le 23 août 2017

Ajouter des réponses ou des commentaires

Pour commenter ceci, se connecter ou s'inscrire