PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

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.

  • Medium
  • Bank of America
  • Coding & Algorithms
  • Data Scientist

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.

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

  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.
Last updated: Jun 26, 2026

Related Coding Questions

  • Compute maximum beauty of a string - Bank of America (Medium)
  • Explain coding solution and alternatives - Bank of America (Medium)

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.