Maximize Item Counts Under Suffix Budget Queries

Read the full interview experience this question came from →

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. ### 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

|Home/Coding & Algorithms/Hudson
Hudson logo
Hudson
Sep 11, 2026
easySoftware EngineerHR ScreenCoding & Algorithms
0
0

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.

Examples

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.

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.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...