00:00

Problem Statement

Input/Output Examples

Visual Explanation

Every group below 1000 is spelled the same way, whatever its scale. That reduces the problem to one helper for 0–999 plus a list of scale words.

Approaches

Both approaches use the same word tables. The first loops over groups of three digits; the second recurses on the largest scale.

Java Solutions

Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.

Complexity Analysis

An int has at most 10 digits, so both approaches do a bounded amount of work. Expressed in terms of the number of digits d, the work is O(d).

Interview Tips and Recommended Approach

  • Handle zero separately. Every other case avoids printing "Zero" inside a number.
  • Skip empty groups. 1000000 is "One Million", not "One Million Zero Thousand".
  • Watch the spaces. Many bugs come from double spaces when a part is empty, as in 1000001.
  • Ask about the format. British English adds "and" ("One Hundred and Five"); confirm which style is expected.

Recommended Approach: Three-Digit Groups

Why: it applies one small helper to each group of three digits, which keeps the special cases in a single place and avoids trimming.