Quick Overview

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.

Convert number words to integer

Company: Bank of America

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Take-home Project

Given a line containing an English phrase representing a number in the range −999,999,999 to 999,999,999, convert it to its integer value. Supported words: negative, zero, one, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen, seventeen, eighteen, nineteen, twenty, thirty, forty, fifty, sixty, seventy, eighty, ninety, hundred, thousand, million. Negative numbers are prefixed with "negative". The word "hundred" is not used when "thousand" could be (e.g., 1500 is "one thousand five hundred"). Print the decimal representation for each input line. Examples: "fifteen" → 15; "negative six hundred thirty eight" → -638.

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.

Given a line containing an English phrase representing a number in the range -999,999,999 to 999,999,999, convert it to its integer value. Supported words: `negative`, `zero`, `one`, `two`, ..., `nineteen`, `twenty`, `thirty`, ..., `ninety`, `hundred`, `thousand`, `million`. Negative numbers are prefixed with the word `negative`. The word `hundred` is not used when `thousand` could be (e.g., 1500 is written "one thousand five hundred", not "fifteen hundred"). Write a function `wordsToNumber(phrase)` that takes the space-separated English phrase as a string and returns its integer value. Examples: - `"fifteen"` -> `15` - `"negative six hundred thirty eight"` -> `-638` - `"one thousand five hundred"` -> `1500`

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

  1. Split the phrase into words. If the first word is 'negative', strip it and remember to negate the final result.
  2. Maintain a 'current' accumulator for the chunk being built. Add units/tens directly; multiply current by 100 on 'hundred'.
  3. On 'thousand' or 'million', flush: add current * scale into a running total and reset current to 0. After the loop, add the leftover current.

Loading coding console...