FastPrepMax Profit
Problems/Max Profit

Max Profit

IBM logoIBM● HardNEW GRADOA

Updated Sep 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: 2026-07-17 — The final 25 in the raw testcase is x, the budget, rather than a sixth cost.

Find the maximum profit that can be generated for the given amount of money. As the answer can be rather large, report the answer as maxProfit % (10 ^ 9 + 7) where % denotes modulo operator.

Complete the function maxProfit in the editor.

maxProfit has the following parameters:

  • int[] cost: an array of integers representing the cost of each item
  • int x: the initial amount of money Walker has

int: the maximum profit that can be obtained modulo (10^9+7)

Function

maxProfit(cost: int[], x: int) → int

Examples

Example 1

cost = [3, 4, 1]x = 8return = 7

:)

Example 2

cost = [19, 78, 27, 15, 20]x = 25return = 16

Walker can afford item 4, whose cost is 20 and whose profit is 2^4 = 16. After buying it, only 5 remains, so none of items 0 through 3 can also be purchased. Any combination that excludes item 4 has profit at most 2^0 + 2^1 + 2^2 + 2^3 = 15. Therefore, the maximum profit is 16.

Example 3

cost = [10, 20, 14, 40, 50]x = 70return = 20

Some possible combination of items Walker can buy are: She can buy items 0, 1, and 2 for a cost of 44 and obtain a profit of 2 ^ 0 + 2 ^ 1 + 2 ^ 2 = 7. She can buy items 0 and 4 for a cost of 60 and obtain a profit of 2 ^ 0 + 2 ^ 4 = 17. She can buy items 1 and 4 for a cost of 70 and obtain a profit of 2 ^ 1 + 2 ^ 4 = 18. She can buy items 2 and 4 for a cost of 64 and obtain a profit of 2 ^ 2 + 2 ^ 4 = 20. Out of all the possible combinations, the maximum profit obtained is by buying items 2 and 4 for a cost of 64 to obtain a profit of 20.

Constraints

  • 1 <= n <= 10^5
  • 1 <= cost[i] <= 10^5
  • 0 <= x <= 10^9

More IBM interview questions

  • Missing IntervalsOA · Seen Nov 2026
  • Last Object Exit TimeOA · Seen Nov 2026
  • Count Peaks and ValleysOA · Seen Nov 2026
  • Count Strictly Increasing Contiguous WindowsOA · Seen Nov 2026
  • Total Each Letter's Count in an Encoded StringOA · Seen Nov 2026
  • Count Servers With No Requests in a Time WindowOA · Seen Nov 2026
  • Alternate Odd and Even Numbers with Two GoroutinesPHONE SCREEN · Seen Nov 2026
  • Longest Substring Without Repeating CharactersPHONE SCREEN · Seen Nov 2026
  • Maximum Easy Tasks After RearrangingOA · Seen Nov 2026
  • Longest Palindromic SubstringPHONE SCREEN · Seen Oct 2026

Similar questions at other companies

  • Delete K Subsets for Maximum ScoreRubrik · OARubrik interview questions
  • Optimize Discount Selection Under a Point BudgetPalantir · OAPalantir interview questions
  • Maximum Index-Weighted Sum with Disjoint SwapsVisa · OAVisa interview questions
  • Submask Sum QueriesIntuit · OAIntuit interview questions
  • Distinct Bitwise-OR Scores of Increasing SubsequencesWalmart · OAWalmart interview questions
  • Match an XOR-Adjusted SumSquarepoint Capital · OASquarepoint Capital interview questions
  • Madam C.J. Walker's Business PlanFortinet · OAFortinet interview questions
  • Count Divisible PermutationsDeloitte · OADeloitte interview questions
  • Get Distinct Goodness ValuesCitadel · OACitadel interview questions
  • Distinct OR Values of Increasing SubsequencesScale AI · OAScale AI interview questions

IBM interview experiences

  • IBM Full-Time SWE OA Questions: 1 Experience
  • IBM Software Intern Résumé Rounds: 2 Experiences (Part 3 of 3)
  • IBM Intern Blitz Interviews: 23 Experiences (Part 2 of 3)
  • IBM Software Intern Interviews: 44 Experiences (Part 1 of 3)
  • IBM Software Intern Interview Questions: 15 Experiences
All IBM interview questionsSee IBM hiring insights
Saved
class Solution {
    public int maxProfit(int[] cost, int x) {
      // write your code here
    }

}
Checking account…