Maximize Item Counts Under Suffix Budget Queries
Company: Hudson
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: HR Screen
Implement `maximum_item_counts(prices, queries)`.
`prices[i]` is the price of item `i`. Each query is `[start, budget]`: you may select any items whose zero-based indices are at least `start`, buying each eligible item at most once. The chosen indices need not be consecutive. Return the maximum number of items that can be bought without spending more than `budget` for each query, in query order.
Queries are independent; a purchase selected for one query does not remove an item from another. Equal prices at different indices still represent different items. For this interface, prices and budgets are nonnegative integers in the same monetary unit, and `start` is between zero and the length of `prices`, inclusive. A start equal to that length leaves no eligible items. No maximum array length or monetary bound is specified. Use the exact integer representation described below for this console interface.
### Exact integer representation
This console represents integer values as canonical nonnegative decimal strings so they retain their exact values in every supported language. This is an input/output representation convention; it does not impose a maximum integer magnitude.
- `prices` is an array of strings representing nonnegative integer prices.
- `queries` remains one array of two-element `[start, budget]` pairs. Both elements of each pair are decimal strings. The numeric value of `start` satisfies the suffix bounds above. Do not split the pairs into separate argument arrays.
- Return an array of decimal strings representing the maximum item counts, in the original query order. No queries produces an empty result array.
- A canonical nonnegative decimal string is `"0"` or a sequence of ASCII digits beginning with `1` through `9`. It has no sign, leading zeroes, or whitespace.
- Compare prices, budgets and spending as exact integer values. Zero-price items are valid and consume none of a query's budget. Do not round, overflow a fixed-width type, or use modular arithmetic.
### Examples
```text
maximum_item_counts(["5","1","3","2"], [["0","4"],["2","4"],["1","0"]]) -> ["2","1","0"]
```
The first query can select prices 1 and 2. The second can select only one of the eligible prices 3 and 2 within its budget. The final query cannot afford any eligible item.
```text
maximum_item_counts(["2","2","7"], [["0","4"],["1","2"],["3","10"]]) -> ["2","1","0"]
```
The first query can buy both separately indexed items priced at 2. The last query has an empty eligible suffix.
Overview: Answer suffix-budget shopping queries by finding the largest affordable item count, allowing nonconsecutive choices and treating each query independently.
Read the full Hudson Software Engineer interview experience this question came from
Implement `maximum_item_counts(prices, queries)`.
`prices[i]` is the price of item `i`. Each query is `[start, budget]`: you may select any items whose zero-based indices are at least `start`, buying each eligible item at most once. The chosen indices need not be consecutive. Return the maximum number of items that can be bought without spending more than `budget` for each query, in query order.
Queries are independent; a purchase selected for one query does not remove an item from another. Equal prices at different indices still represent different items. For this interface, prices and budgets are nonnegative integers in the same monetary unit, and `start` is between zero and the length of `prices`, inclusive. A start equal to that length leaves no eligible items. No maximum array length or monetary bound is specified. Use the exact integer representation described below for this console interface.
### Exact integer representation
This console represents integer values as canonical nonnegative decimal strings so they retain their exact values in every supported language. This is an input/output representation convention; it does not impose a maximum integer magnitude.
- `prices` is an array of strings representing nonnegative integer prices.
- `queries` remains one array of two-element `[start, budget]` pairs. Both elements of each pair are decimal strings. The numeric value of `start` satisfies the suffix bounds above. Do not split the pairs into separate argument arrays.
- Return an array of decimal strings representing the maximum item counts, in the original query order. No queries produces an empty result array.
- A canonical nonnegative decimal string is `"0"` or a sequence of ASCII digits beginning with `1` through `9`. It has no sign, leading zeroes, or whitespace.
- Compare prices, budgets and spending as exact integer values. Zero-price items are valid and consume none of a query's budget. Do not round, overflow a fixed-width type, or use modular arithmetic.
### Examples
```text
maximum_item_counts(["5","1","3","2"], [["0","4"],["2","4"],["1","0"]]) -> ["2","1","0"]
```
The first query can select prices 1 and 2. The second can select only one of the eligible prices 3 and 2 within its budget. The final query cannot afford any eligible item.
```text
maximum_item_counts(["2","2","7"], [["0","4"],["1","2"],["3","10"]]) -> ["2","1","0"]
```
The first query can buy both separately indexed items priced at 2. The last query has an empty eligible suffix.
Constraints
- prices and both elements of every query pair are canonical nonnegative decimal strings.
- Every start represents an index from zero through len(prices), inclusive.
- queries remains one list of two-element [start,budget] pairs; results are decimal strings in the same order.
- Each query independently buys each eligible indexed item at most once; zero-price and duplicate-price items are valid.
- No queries returns an empty list; no maximum length or integer magnitude is added.
Examples
Input: (['5', '1', '3', '2'], [['0', '4'], ['2', '4'], ['1', '0']])
Expected Output: ['2', '1', '0']
Explanation: First source example: choose any eligible subset independently.
Input: (['2', '2', '7'], [['0', '4'], ['1', '2'], ['3', '10']])
Expected Output: ['2', '1', '0']
Explanation: Second source example: equal-priced indexed items remain distinct and a suffix can be empty.
Hints
- The eligible items begin at the stated index, but a selected set need not be contiguous.
- Purchases for one query do not affect any other query.