Convert number words to integer
Company: Bank of America
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Take-home Project
Quick Answer: This question evaluates textual number parsing, tokenization, and numeric mapping skills, testing string manipulation and lexical interpretation within the Coding & Algorithms domain for a Data Scientist role and emphasizing practical application rather than pure theoretical concepts.
Constraints
- The integer value is in the range -999,999,999 to 999,999,999.
- Negative numbers are prefixed with the word 'negative'.
- 'hundred' is not used when 'thousand' could be (e.g., 1500 is 'one thousand five hundred').
- Words are lowercase and space-separated.
- Supported scale words: hundred, thousand, million.
Examples
Input: ("fifteen",)
Expected Output: 15
Explanation: A single teen word maps directly to 15.
Input: ("negative six hundred thirty eight",)
Expected Output: -638
Explanation: 'six' * 100 = 600, plus 'thirty' (30) plus 'eight' (8) = 638, negated to -638.
Input: ("zero",)
Expected Output: 0
Explanation: Boundary: the word 'zero' maps to 0.
Input: ("one thousand five hundred",)
Expected Output: 1500
Explanation: 'one' * 1000 flushed = 1000; then 'five' * 100 = 500; total 1500. (Not written 'fifteen hundred'.)
Input: ("negative nine hundred ninety nine million nine hundred ninety nine thousand nine hundred ninety nine",)
Expected Output: -999999999
Explanation: Maximum-magnitude negative value, exercising million + thousand + hundred chunks together.
Input: ("two million three hundred forty five thousand six hundred seventy eight",)
Expected Output: 2345678
Explanation: Mixed million/thousand/hundred chunks compose 2,345,678.
Input: ("twenty",)
Expected Output: 20
Explanation: A standalone tens word maps to 20.
Input: ("one hundred",)
Expected Output: 100
Explanation: 'one' * 100 = 100, with no thousand/million flush.
Hints
- Split the phrase into words. If the first word is 'negative', strip it and remember to negate the final result.
- Maintain a 'current' accumulator for the chunk being built. Add units/tens directly; multiply current by 100 on 'hundred'.
- On 'thousand' or 'million', flush: add current * scale into a running total and reset current to 0. After the loop, add the leftover current.