FastPrepMost Books Within a Budget
Problems/Most Books Within a Budget

Most Books Within a Budget

Indeed logoIndeed● MediumFULLTIMENEW GRADOA
QuestionFunctionExamplesConstraints

Problem statement

Source note: Two Indeed Prime platform reports, one of which says it is not Indeed's own hiring test, ask for the most books a budget can buy from bundles that each have a book count and a price. A bundle can be bought more than once, which the author confirmed: bundles of 20 books for 24 and 19 books for 20 with a budget of 50 give 40.

The reports give no limits. Here the budget is at most 10^4, there are at most 100 bundles, each bundle has 1 to 1000 books and costs 1 to 10^4, and you return 0 when no bundle fits.

The judged task matches the reports at about 95%.

A bookstore sells books only in bundles. Bundle i holds bundleQuantities[i] books and costs bundleCosts[i] dollars for the whole bundle. You can buy each bundle as many times as you like, or not at all.

You have a budget of n dollars. Return the largest total number of books you can buy without spending more than n dollars. You do not have to spend the whole budget. If no bundle fits in the budget, return 0.

Function

budgetShopping(n: int, bundleQuantities: int[], bundleCosts: int[]) → int

Examples

Example 1

n = 50bundleQuantities = [20,19]bundleCosts = [24,20]return = 40

Two bundles of 20 books cost 2 * 24 = 48, which fits in 50, for 40 books. Two bundles of 19 books cost 40 and give only 38 books, and a third one would cost 60. One of each costs 44 and gives 39 books. The answer is 40.

Example 2

n = 10bundleQuantities = [5,3]bundleCosts = [6,4]return = 8

One 5-book bundle and one 3-book bundle cost 6 + 4 = 10 and give 8 books. Two 3-book bundles cost 8 and give only 6 books, and two 5-book bundles cost 12, which is too much. The answer is 8.

Example 3

n = 3bundleQuantities = [7,2]bundleCosts = [5,4]return = 0

Every bundle costs more than the budget of 3, so you buy nothing. The answer is 0.

Constraints

  • 1 <= n <= 10^4
  • 1 <= bundleQuantities.length == bundleCosts.length <= 100
  • 1 <= bundleQuantities[i] <= 1000
  • 1 <= bundleCosts[i] <= 10^4

More Indeed problems

  • Insert into an Unrolled Linked ListPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Course Completion OrderPHONE SCREEN · Seen May 2025
  • Recipes You Can PreparePHONE SCREEN · Seen May 2025
All Indeed interview questionsSee Indeed hiring insights
Saved
class Solution {
    public int budgetShopping(int n, int[] bundleQuantities, int[] bundleCosts) {
        // Write your code here.
    }
}
Checking account…