Scale Recipe Quantities Without Changing Product Descriptions
Company: Upstart
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Online Assessment
Overview: Multiply only the leading quantity in each ingredient line, preserving recipe order, product wording, and numbers inside descriptions.
Constraints
- 1 <= nb_persons <= 1000000
- 0 <= len(ingredients) <= 10000
- Each leading quantity is an ASCII decimal integer from 1 through 1000000, written without leading zeros.
- The first space of a line separates that quantity from a nonempty product description of at most 200 ASCII characters; the description may itself contain digits and spaces.
- Every character after that first space is preserved exactly.
- Do not change singular or plural wording; numbers inside the product description are part of the description and remain unchanged.
- Format the multiplied quantity as ordinary decimal digits without unnecessary leading zeros.
- Intermediate products may exceed a signed 32-bit integer: quantity * nb_persons <= 10^12.
- The returned list has the same length as ingredients and keeps the input line order.
Examples
Input: (5, [])
Expected Output: []
Explanation: Minimum valid input: no ingredient lines, so the result is the empty list.
Input: (1, ['2 eggs'])
Expected Output: ['2 eggs']
Explanation: Singleton with nb_persons = 1: 2 * 1 = 2, so the line is unchanged.
Hints
- Decide first where a line's quantity ends. The statement names exactly one boundary, and it is the first space, not any space.
- Treat everything after that boundary as opaque text: copy it rather than re-deriving it from pieces, so its own digits and spacing survive untouched.
- Check how large a product can get before choosing the numeric type in each language.