Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
Convert a non-negative integer num to its English words representation.
Example 1:
Input: num = 123 Output: "One Hundred Twenty Three"
Example 2:
Input: num = 12345 Output: "Twelve Thousand Three Hundred Forty Five"
Example 3:
Input: num = 1234567 Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
Constraints:
0 <= num <= 231 - 1Problem summary: Convert a non-negative integer num to its English words representation. Example 1: Input: num = 123 Output: "One Hundred Twenty Three" Example 2: Input: num = 12345 Output: "Twelve Thousand Three Hundred Forty Five" Example 3: Input: num = 1234567 Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
123
12345
1234567
integer-to-roman)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #273: Integer to English Words
class Solution {
private String[] lt20 = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen",
"Seventeen", "Eighteen", "Nineteen"};
private String[] tens
= {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
private String[] thousands = {"Billion", "Million", "Thousand", ""};
public String numberToWords(int num) {
if (num == 0) {
return "Zero";
}
StringBuilder sb = new StringBuilder();
for (int i = 1000000000, j = 0; i > 0; i /= 1000, ++j) {
if (num / i == 0) {
continue;
}
sb.append(transfer(num / i)).append(thousands[j]).append(' ');
num %= i;
}
return sb.toString().trim();
}
private String transfer(int num) {
if (num == 0) {
return "";
}
if (num < 20) {
return lt20[num] + " ";
}
if (num < 100) {
return tens[num / 10] + " " + transfer(num % 10);
}
return lt20[num / 100] + " Hundred " + transfer(num % 100);
}
}
// Accepted solution for LeetCode #273: Integer to English Words
func numberToWords(num int) string {
if num == 0 {
return "Zero"
}
lt20 := []string{
"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen",
"Sixteen", "Seventeen", "Eighteen", "Nineteen",
}
tens := []string{
"", "Ten", "Twenty", "Thirty", "Forty", "Fifty",
"Sixty", "Seventy", "Eighty", "Ninety",
}
thousands := []string{"Billion", "Million", "Thousand", ""}
var transfer func(int) string
transfer = func(num int) string {
if num == 0 {
return ""
}
if num < 20 {
return lt20[num]
}
if num < 100 {
if num%10 == 0 {
return tens[num/10]
}
return tens[num/10] + " " + transfer(num%10)
}
if num%100 == 0 {
return lt20[num/100] + " Hundred"
}
return lt20[num/100] + " Hundred " + transfer(num%100)
}
res := ""
for i, j := 1000000000, 0; i > 0; i, j = i/1000, j+1 {
cur := num / i
if cur == 0 {
continue
}
if res != "" {
res += " "
}
res += transfer(cur)
if thousands[j] != "" {
res += " " + thousands[j]
}
num %= i
}
return res
}
# Accepted solution for LeetCode #273: Integer to English Words
class Solution:
def numberToWords(self, num: int) -> str:
if num == 0:
return 'Zero'
lt20 = [
'',
'One',
'Two',
'Three',
'Four',
'Five',
'Six',
'Seven',
'Eight',
'Nine',
'Ten',
'Eleven',
'Twelve',
'Thirteen',
'Fourteen',
'Fifteen',
'Sixteen',
'Seventeen',
'Eighteen',
'Nineteen',
]
tens = [
'',
'Ten',
'Twenty',
'Thirty',
'Forty',
'Fifty',
'Sixty',
'Seventy',
'Eighty',
'Ninety',
]
thousands = ['Billion', 'Million', 'Thousand', '']
def transfer(num):
if num == 0:
return ''
if num < 20:
return lt20[num] + ' '
if num < 100:
return tens[num // 10] + ' ' + transfer(num % 10)
return lt20[num // 100] + ' Hundred ' + transfer(num % 100)
res = []
i, j = 1000000000, 0
while i > 0:
if num // i != 0:
res.append(transfer(num // i))
res.append(thousands[j])
res.append(' ')
num %= i
j += 1
i //= 1000
return ''.join(res).strip()
// Accepted solution for LeetCode #273: Integer to English Words
struct Solution;
impl Solution {
fn number_to_words(num: i32) -> String {
if num == 0 {
return "Zero".to_string();
}
let nineteen: &'static str = "One Two Three Four Five Six Seven Eight Nine Ten Eleven Twelve Thirteen Fourteen Fifteen Sixteen Seventeen Eighteen Nineteen";
let nineteen: Vec<&'static str> = nineteen.split_whitespace().collect();
let tens: &'static str = "Twenty Thirty Forty Fifty Sixty Seventy Eighty Ninety";
let tens: Vec<&'static str> = tens.split_whitespace().collect();
let units: &'static str = "Hundred Thousand Million Billion";
let units: Vec<&'static str> = units.split_whitespace().collect();
Self::words(num as usize, &nineteen, &tens, &units).join(" ")
}
fn words(
num: usize,
nineteen: &[&'static str],
tens: &[&'static str],
units: &[&'static str],
) -> Vec<&'static str> {
if num < 20 {
if num > 0 {
vec![nineteen[num - 1]]
} else {
vec![]
}
} else if num < 100 {
let d = num / 10;
vec![
if d > 1 { vec![tens[d - 2]] } else { vec![] },
Self::words(num % 10, nineteen, tens, units),
]
.concat()
} else if num < 1000 {
vec![
Self::words(num / 100, nineteen, tens, units),
vec![units[0]],
Self::words(num % 100, nineteen, tens, units),
]
.concat()
} else if num < 1_000_000 {
vec![
Self::words(num / 1000, nineteen, tens, units),
vec![units[1]],
Self::words(num % 1000, nineteen, tens, units),
]
.concat()
} else if num < 1_000_000_000 {
vec![
Self::words(num / 1_000_000, nineteen, tens, units),
vec![units[2]],
Self::words(num % 1_000_000, nineteen, tens, units),
]
.concat()
} else {
vec![
Self::words(num / 1_000_000_000, nineteen, tens, units),
vec![units[3]],
Self::words(num % 1_000_000_000, nineteen, tens, units),
]
.concat()
}
}
}
#[test]
fn test() {
let num = 123;
let res = "One Hundred Twenty Three".to_string();
assert_eq!(Solution::number_to_words(num), res);
let num = 12345;
let res = "Twelve Thousand Three Hundred Forty Five".to_string();
assert_eq!(Solution::number_to_words(num), res);
let num = 1_234_567;
let res = "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven".to_string();
assert_eq!(Solution::number_to_words(num), res);
let num = 1_234_567_891;
let res = "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One".to_string();
assert_eq!(Solution::number_to_words(num), res);
let num = 20;
let res = "Twenty".to_string();
assert_eq!(Solution::number_to_words(num), res);
}
// Accepted solution for LeetCode #273: Integer to English Words
function numberToWords(num: number): string {
if (num === 0) {
return 'Zero';
}
const lt20 = [
'',
'One',
'Two',
'Three',
'Four',
'Five',
'Six',
'Seven',
'Eight',
'Nine',
'Ten',
'Eleven',
'Twelve',
'Thirteen',
'Fourteen',
'Fifteen',
'Sixteen',
'Seventeen',
'Eighteen',
'Nineteen',
];
const tens = [
'',
'Ten',
'Twenty',
'Thirty',
'Forty',
'Fifty',
'Sixty',
'Seventy',
'Eighty',
'Ninety',
];
const thousands = ['Billion', 'Million', 'Thousand', ''];
const transfer = (n: number): string => {
if (n === 0) {
return '';
}
if (n < 20) {
return lt20[n];
}
if (n < 100) {
if (n % 10 === 0) {
return tens[Math.floor(n / 10)];
}
return tens[Math.floor(n / 10)] + ' ' + transfer(n % 10);
}
if (n % 100 === 0) {
return lt20[Math.floor(n / 100)] + ' Hundred';
}
return lt20[Math.floor(n / 100)] + ' Hundred ' + transfer(n % 100);
};
let res = '';
for (let i = 1_000_000_000, j = 0; i > 0; i = Math.floor(i / 1000), ++j) {
const cur = Math.floor(num / i);
if (cur === 0) {
continue;
}
if (res !== '') {
res += ' ';
}
res += transfer(cur);
if (thousands[j] !== '') {
res += ' ' + thousands[j];
}
num %= i;
}
return res;
}
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.