IPO

HardHeapheap

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You start with capital w and may finish at most k distinct projects. Project i requires you to currently have at least capital[i] to start it, and completing it adds its pure profit profits[i] to your capital (the requirement is not spent). Projects are done one after another, so profits earned early can unlock more expensive projects later. Return the maximum capital you can end with. Aim for O((n + k) log n) time.

Examples

Input: k = 2, w = 0, profits = [1,2,3], capital = [0,1,1]
Output: 4
Only project 0 is affordable at first (capital 1 after it); then take project 2 for a final capital of 4.
Input: k = 3, w = 0, profits = [1,2,3], capital = [0,1,2]
Output: 6
Do projects 0, 1, 2 in order: 0 -> 1 -> 3 -> 6.

Constraints

  • 1 <= k <= 10^5
  • 0 <= w <= 10^9
  • n == profits.length == capital.length
  • 1 <= n <= 10^5
  • 0 <= profits[i] <= 10^4
  • 0 <= capital[i] <= 10^9

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More heap problems