Spell Out a Nonnegative Integer
CarvanaSoftware Engineer · OnsiteLast updated
This question evaluates a candidate's ability to decompose integers and implement precise string-formatting logic, including mapping numeric values to their English word equivalents and managing scale units and boundary conditions.
Given a nonnegative integer, return its English words representation.
Implement a function that handles numbers from 0 through 2,147,483,647 inclusive.
Requirements:
- Return
"Zero"for input0. - Use standard American English scale words:
Thousand,Million, andBillion. - Do not include the word
and. - Use single spaces between words and no leading or trailing spaces.
- Capitalize each word as shown in the examples.
Examples:
- Input:
7Output:"Seven" - Input:
19Output:"Nineteen" - Input:
123Output:"One Hundred Twenty Three" - Input:
1005Output:"One Thousand Five" - Input:
1234567Output:"One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven" - Input:
2147483647Output:"Two Billion One Hundred Forty Seven Million Four Hundred Eighty Three Thousand Six Hundred Forty Seven"
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.