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.
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.