Coding questions · Upstart
Scale Recipe Quantities Without Changing Product Descriptions
Multiply only the leading quantity in each ingredient line, preserving recipe order, product wording, and numbers inside descriptions.
Scale a one-person recipe for nb_persons people. Each ingredient line begins with a positive integer quantity, followed by one space and a product description. Multiply only the leading quantity; preserve the description and line order.
Function Contract
Implement adjust_quantities(nb_persons, ingredients) -> list[str].
Do not change singular or plural wording. Numbers inside the product description are part of that description and must remain unchanged.
Constraints and Clarifications
The numeric and size bounds below are explicit practice bounds for the stated format.
1 <= nb_persons <= 1000000.0 <= len(ingredients) <= 10000.- Each leading quantity is an ASCII decimal integer from
1through1000000. - The first space separates that quantity from a nonempty product description of at most 200 ASCII characters.
- Preserve every character after that first space, including other digits and spaces.
- Format the multiplied quantity as ordinary decimal digits without unnecessary leading zeros.
- Intermediate products may exceed a signed 32-bit integer.
Examples
nb_persons = 3
ingredients = ["2 eggs", "200 grams of flour", "150 grams of sugar", "1 liter(s) of milk"]
Output: ["6 eggs", "600 grams of flour", "450 grams of sugar", "3 liter(s) of milk"]
nb_persons = 4
ingredients = ["2 packs of 6 rolls", "1 bottle of 2 percent milk"]
Output: ["8 packs of 6 rolls", "4 bottle of 2 percent milk"]
Hint: Split only at the quantity boundary
The first space identifies the numeric prefix. Parsing every number in a line would also change numbers belonging to the product description.
Scale Recipe Quantities for a Given Number of People
A recipe is written for one person. Each ingredient is a single line that begins with a positive integer quantity, then exactly one space, then a product description.
Given nb_persons and the list ingredients, return a new list in which only the leading quantity of each line has been multiplied by nb_persons. The product description and the order of the lines must be preserved.
Rules:
- The first space of a line is the boundary between the quantity and the description. Every character after that first space is preserved exactly, including further digits and further spaces.
- Numbers that appear inside a product description belong to the description and must remain unchanged.
- Do not change singular or plural wording.
- Format each multiplied quantity as ordinary decimal digits with no unnecessary leading zeros.
- The returned list has the same length as
ingredientsand the same line order.
A multiplied quantity can exceed 2^31 - 1: it can be as large as 10^12. Java must therefore use long and C++ must use long long for the quantity and the product.
Examples
Example 1:
nb_persons = 3
ingredients = ["2 eggs", "200 grams of flour", "150 grams of sugar", "1 liter(s) of milk"]
Output: ["6 eggs", "600 grams of flour", "450 grams of sugar", "3 liter(s) of milk"]
Example 2:
nb_persons = 4
ingredients = ["2 packs of 6 rolls", "1 bottle of 2 percent milk"]
Output: ["8 packs of 6 rolls", "4 bottle of 2 percent milk"]
In Example 2 the 6 in "6 rolls" and the 2 in "2 percent milk" are part of the descriptions, so they are left alone, and "1 bottle" becomes "4 bottle" without being re-worded.
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.
Approach
Each line is processed independently, so the whole task is a single left-to-right pass over the list.
Algorithm: for a line, locate the index of its FIRST space. The substring before that index is the quantity token; the substring after that index is the description, taken verbatim to the end of the line. Parse the quantity token as an integer, multiply it by nb_persons, render the product with the language's ordinary decimal integer-to-string conversion (which never emits leading zeros), and concatenate product + one space + description. Append the new line to the output list.
Invariant: after processing the first k lines, the output list holds exactly k strings, the i-th of which equals str(q_i * nb_persons) + ' ' + d_i, where q_i and d_i are the quantity and description of input line i. Because the output is only ever appended to, position i of the output corresponds to position i of the input, which is what preserves line order.
Correctness: splitting at the first space is exactly the boundary the statement defines, so d_i is preserved byte for byte, including any digits and any additional spaces it contains. No wording is ever rewritten, so singular/plural forms are untouched by construction. Only q_i participates in arithmetic, so numbers inside descriptions cannot change.
Edge cases: an empty ingredients list returns an empty list (the loop body never runs). nb_persons = 1 returns lines that are byte-identical to the input, since the decimal rendering of q_i * 1 has no leading zeros and neither does q_i. A description that itself starts with digits (for example '2 500 ml bottles') is safe because only the prefix before the first space is parsed. Multiple consecutive spaces after the boundary survive because the description is taken as a raw slice rather than re-joined from tokens.
Overflow: with nb_persons <= 10^6 and each quantity <= 10^6, the product is at most 10^12. That overflows signed 32-bit, so Java uses long and C++ uses long long; 10^12 is far below 2^53, so JavaScript's Number arithmetic and String() conversion remain exact and never switch to exponential notation.
- Time complexity: O(total input length), i.e. O(sum of the lengths of all ingredient lines); each line is scanned and rebuilt a constant number of times.
- Space complexity: O(total input length) for the returned list of rebuilt lines; O(max line length) of auxiliary space beyond the output.