Scale Ingredient Quantities
Company: Upstart
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Take-home Project
# Scale Ingredient Quantities
Each ingredient is a string in the exact format `"<quantity> <unit> <name>"`. Multiply every quantity by `servings` and return the scaled strings in the original order.
### Function Signature
```python
def scale_ingredients(ingredients: list[str], servings: int) -> list[str]:
...
```
### Example
```text
Input: ingredients = ["100 g flour", "50 g water", "10 g sugar"]
servings = 8
Output: ["800 g flour", "400 g water", "80 g sugar"]
```
### Constraints
- `0 <= len(ingredients) <= 100_000`
- `0 <= servings <= 10^9`
- Each quantity is a nonnegative integer no greater than `10^9`.
- `unit` is one nonempty token; `name` is nonempty and may contain spaces.
- Fields are separated by single spaces with no leading or trailing spaces.
### Clarifications
- The input quantities describe one serving; scale directly by the supplied serving count.
- Preserve the unit, ingredient name, and input order exactly.
- For `servings == 0`, every returned quantity is `0`.
- Return a new list and do not mutate `ingredients`.
### Hints
- Split only enough times to isolate the first two fields from the remaining name.
Quick Answer: Scale ingredient strings by multiplying each leading integer quantity by a serving count while preserving unit, multiword name, and input order. Split only enough fields to isolate the quantity and unit, return a new list, and handle zero servings exactly.
Each ingredient string has quantity, unit, and name fields separated by single spaces. Multiply the integer quantity by servings and return newly formatted strings in the original order while preserving each unit and full ingredient name.
Constraints
- servings is a nonnegative integer.
- Every quantity is a nonnegative integer.
- The unit is one nonempty token.
- The name is nonempty and may contain spaces.
- Do not mutate or reorder the input.
Examples
Input: ([], 4)
Expected Output: []
Explanation: An empty ingredient list remains empty.
Input: (["100 g flour", "50 g water", "10 g sugar"], 8)
Expected Output: ["800 g flour", "400 g water", "80 g sugar"]
Explanation: The prompt quantities scale by eight.
Hints
- Split each string at most twice.
- Convert only the first field to an integer.
- Preserve the second field and entire remaining suffix exactly.