Bakery Counter Decisions
Mark which ordered value requests can consume matching array elements
A bakery has a collection of pastries ready to sell. Each pastry has an integer recipe code. The array pastries contains one code for each available pastry, so repeated codes represent multiple pastries of the same recipe.
Customers arrive in the order given by requests. Each customer asks for exactly one pastry with the specified recipe code:
- If a matching pastry is still available, sell one to that customer and mark the request as accepted.
- Otherwise, mark the request as rejected. A rejected request does not change the inventory.
No new pastries are added while these requests are processed.
Return a boolean array in the same order as requests, where true means the request was accepted and false means it was rejected. Recipe codes are identifiers; negative codes are valid.
Examples
Example 1
Input: pastries = [4,7,4], requests = [4,7,9,4,4] Output: [true,true,false,true,false]
There are two pastries with code 4 and one with code 7. The first requests for codes 4 and 7 are accepted. Code 9 is unavailable. The next request for code 4 uses its last pastry, so the final request for code 4 is rejected.
Example 2
Input: pastries = [], requests = [3,3] Output: [false,false]
The bakery starts with no pastries, so neither customer can receive the requested recipe.
Example 3
Input: pastries = [-2,0], requests = [-2,-2,0] Output: [true,false,true]
The first request for code -2 is accepted. Only one pastry has that code, so the second request is rejected. The final request uses the available pastry with code 0.
Constraints
- 0 <= pastries.length <= 10000
- 0 <= requests.length <= 10000
- -1000000000 <= pastries[i], requests[i] <= 1000000000
The intended solution takes expected O(n + m) time and O(k) auxiliary space, excluding the returned array, where n is the number of pastries, m is the number of requests, and k is the number of distinct recipe codes in the initial inventory.
Hints
Show hint 1Hint 1
What information about the available pastries is sufficient to decide whether a request can be accepted?
Show hint 2Hint 2
Store the available quantity for each recipe code, and decrease it only when a request is accepted.
Follow-up questions
What an interviewer might ask once you have a working solution.
- How would you support deliveries of additional pastries between customer requests?
- How would the solution change if each request specified both a recipe code and a quantity, and a request had to be fulfilled completely or rejected?
Practice this with an AI interviewer
Explain your approach out loud, write Python or JavaScript, run it against hidden tests (including large inputs), and get a scored debrief.
Start this problem