Convert the Digits of an Integer to English Words

Quick Overview

Write a function that takes a non-negative integer and returns the English word for each of its digits, in order, separated by single spaces. It tests digit extraction, lookup tables and exact output formatting, including the case where the input is zero.

Convert the Digits of an Integer to English Words

Company: Mastercard

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a non-negative integer `n`, return a string containing the English word for each digit of `n`, separated by single spaces, in the order the digits appear. The digits map to words as follows: `0` -> "Zero", `1` -> "One", `2` -> "Two", `3` -> "Three", `4` -> "Four", `5` -> "Five", `6` -> "Six", `7` -> "Seven", `8` -> "Eight", `9` -> "Nine". For example, if `n = 302`, the function should return `"Three Zero Two"`. ### Function Signature ```python def convert_digits_to_words(n: int) -> str: ``` ### Rules - Use each word exactly as spelled and capitalized in the mapping above. - Separate consecutive words with exactly one space. The result has no leading or trailing space. - `n` is read in base 10 without leading zeros, so `n = 0` produces the single word "Zero". ### Constraints - `0 <= n <= 2^31 - 1` ### Examples ```text Input: n = 302 Output: "Three Zero Two" ``` The digits are 3, 0 and 2, which map to "Three", "Zero" and "Two". The zero in the middle still gets its own word. ```text Input: n = 1234 Output: "One Two Three Four" ``` The digits 1, 2, 3 and 4 map to "One", "Two", "Three" and "Four", kept in the order they appear. ```text Input: n = 0 Output: "Zero" ``` Zero has one digit, so the result is a single word with no spaces.

Overview: Write a function that takes a non-negative integer and returns the English word for each of its digits, in order, separated by single spaces. It tests digit extraction, lookup tables and exact output formatting, including the case where the input is zero.

Mastercard logo
Mastercard
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given a non-negative integer n, return a string containing the English word for each digit of n, separated by single spaces, in the order the digits appear.

The digits map to words as follows: 0 -> "Zero", 1 -> "One", 2 -> "Two", 3 -> "Three", 4 -> "Four", 5 -> "Five", 6 -> "Six", 7 -> "Seven", 8 -> "Eight", 9 -> "Nine".

For example, if n = 302, the function should return "Three Zero Two".

Function Signature

def convert_digits_to_words(n: int) -> str:

Rules

  • Use each word exactly as spelled and capitalized in the mapping above.
  • Separate consecutive words with exactly one space. The result has no leading or trailing space.
  • n is read in base 10 without leading zeros, so n = 0 produces the single word "Zero".

Constraints

  • 0 <= n <= 2^31 - 1

Examples

Input: n = 302
Output: "Three Zero Two"

The digits are 3, 0 and 2, which map to "Three", "Zero" and "Two". The zero in the middle still gets its own word.

Input: n = 1234
Output: "One Two Three Four"

The digits 1, 2, 3 and 4 map to "One", "Two", "Three" and "Four", kept in the order they appear.

Input: n = 0
Output: "Zero"

Zero has one digit, so the result is a single word with no spaces.