Quick Overview

Answer suffix-budget shopping queries by finding the largest affordable item count, allowing nonconsecutive choices and treating each query independently.

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

  1. The eligible items begin at the stated index, but a selected set need not be contiguous.
  2. Purchases for one query do not affect any other query.

Loading coding console...

Show the approach

Approach

Parse all prices as exact integers. For each query, copy its eligible suffix, sort that copy by numerical price and buy entries in ascending order while the next one fits the remaining exact budget. The cheapest r eligible items have cost no greater than any other r-item subset, by exchanging more expensive selected items for cheaper unselected ones. Therefore every purchased prefix is feasible, and when its next price exceeds the remaining budget, no larger subset can be feasible. Nonnegative prices make the stopping rule valid, and zero-price entries are always included even at zero budget. Copying each suffix and resetting its budget preserves query independence and duplicate indexed items; appending each count preserves query order. The implementation is deliberately per-query and imposes no unstated data-size promise. Monetary values use Python int, JavaScript BigInt, Java BigInteger or a standard-library-only C++ signed limb integer. Python parses at most nine decimal digits at a time, retaining the full stated magnitude domain despite its default decimal-conversion limit. Native start/count types address the materialized containers, while no monetary value is narrowed.

Time complexity:
O(n) price parses plus O(sum over queries of m log m) exact price comparisons and O(sum m) exact subtraction/check operations, where m is the eligible suffix length; decimal digit costs are additional.
Space complexity:
O(n) parsed prices plus O(m) copied values for one query, the output list and arbitrary-precision digit storage.