Grokking Oracle Coding Interview
Ask Author
Back to course home

0% completed

Vote For New Content
Solution: Integer to English Words
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Problem Statement

Given a positive integer num, return the English word representation of num.

Examples

  • Example 1:

    • Input: num = 12345
    • Expected Output: "Twelve Thousand Three Hundred Forty Five"
    • Justification: The number 12345 is broken down into "Twelve Thousand" for the first two digits, followed by "Three Hundred" for the next digit, and "Forty Five" for the last two digits.
  • Example 2:

    • Input: num = 1001
    • Expected Output: "One Thousand One"
    • Justification: The number 1001 translates to "One Thousand" for the first digit and "One" for the last digit, skipping the hundreds and tens place because they are zeros.
  • Example 3:

    • Input: num = 2000000
    • Expected Output: "Two Million"
    • Justification: The number 2000000 is simply "Two Million" as there are no other numbers to represent after the word "Million".

Solution

To solve this problem, the approach involves breaking down the given number into manageable chunks, each representing a part of the number in words. This method is effective because it mirrors how humans naturally interpret numbers. By dividing the number into segments such as billions, millions, thousands, and then the last three digits, we can systematically convert each segment into words using predefined mappings for numbers 1-19, tens (20, 30, ..., 90), and the significant place values (thousand, million, billion). This approach ensures that we can handle any non-negative integer input systematically and efficiently.

The algorithm's effectiveness lies in its simplicity and scalability. By treating the number in segments, we can reuse the same logic for each segment, making the code more maintainable and easier to understand. This method also scales well for large numbers, as it applies the same logic regardless of the number's size, ensuring consistency in the output.

Step-by-Step Algorithm

  1. Initialization: Define three arrays or lists for mapping numbers to words. These include:

    • lessThan20 for numbers 1 to 19.
    • tens for multiples of ten from 20 to 90.
    • thousands for the place values thousand, million, and billion.
  2. Check for Zero: If the input number is 0, immediately return the word "Zero".

  3. Conversion Process:

    • Initialize an empty string words to hold the final words representation.
    • Initialize a counter i to keep track of the current place value (thousands, millions, etc.).
    • While the input number is greater than 0, perform the following steps:
      • If the last three digits of the current number segment (num % 1000) are not zero, convert this segment to words using a helper function and append the appropriate place value from the thousands array.
      • Divide the number by 1000 to move to the next segment.
      • Increment the place value counter i.
  4. Helper Function:

    • This function takes a number less than 1000 and converts it into words.
    • If the number is less than 20, return the corresponding word from lessThan20.
    • If the number is between 20 and 99, return the word for the tens place from tens and recursively call the helper function for the remainder (units place).
    • If the number is between 100 and 999, return the word for the hundreds place from lessThan20, the word "Hundred", and recursively call the helper function for the remainder (less than 100).
  5. Finalization:

    • Trim any leading or trailing spaces from the words string.
    • Return the words string as the final English words representation of the input number.

Algorithm Walkthrough

Let's consider the input: num = "12345"

  1. Initialization: The mappings for numbers to words are defined in arrays/lists as per the algorithm.

  2. Check for Zero: The input number is 12345, which is not zero, so we proceed.

  3. Conversion Process:

    • The input number 12345 is greater than 0, so we start the conversion.
    • First Iteration:
      • The last three digits are 345. Next, we convert 345 to words.
      • We call the helper function with 345.
        • The number is greater than 100, so we get "Three Hundred " + helper(45).
        • For 45, it falls in the less than 100 category. So, we get "Forty " + helper(45%10).
        • For 5, it falls in the less than 20 category. So, we get "Five".
        • The final return for this segment is "Three Hundred Forty Five".
      • We append "" (empty string) after converting 345 to words because i = 0 (first segment after the units).
      • The words string remains same "Three Hundred Forty Five ".
      • We divide the number by 1000, updating it to 12.
    • Second Iteration:
      • Now, the number is 12.
      • Since 12 % 1000 = 12 is not zero, we convert 12 to words using the helper function.
        • 12 is less than 20, so we directly get "Twelve" from lessThan20.
      • Prepand words string with thousands[1] and helper(12).
      • The words string is updated to "Twelve Thousand Three Hundred Forty Five ".
    • The loop ends as the number is now 0.
  4. Finalization:

    • Trim the final words string to remove any trailing space.
    • The final output is "Twelve Thousand Three Hundred Forty Five".

Code

Python3
Python3

. . . .

Complexity Analysis

Time Complexity

The algorithm has a linear time complexity relative to the number of digits in the input number. In the worst case, it processes each group of three digits once, leading to a time complexity of O(n/3) = O(n), where n is the number of digits.

Space Complexity

The space complexity is O(1) for the storage of mappings (lessThan20, tens, thousands), which does not scale with the input size. However, the recursive calls in the helper function use stack space, but since the depth is limited to a maximum of 3 (for numbers less than 1000), it can also be considered O(1).

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible