KatInterview

Prime-Step Path Score + Bottleneck Throughput Boost

Dynamic ProgrammingAuto-graded · 21 test cases
MediumAuto-gradedDynamic Programmingdynamic-programmingbinary-searchnumber-theory

Last updated

Tests DP over prime-length jumps on an array and binary search on a "maximize the minimum" pipeline scaling problem, combined into one function.

You must solve two independent sub-problems. Return a list [path_score, max_throughput] where each element is the answer to one sub-problem.


Sub-problem 1: Collect Points Along a Prime-Step Path

You have an integer array points of length n and a step limit k. Your journey begins at index 0, and your running total starts at points[0].

At each step you move from your current index i to some later index j subject to all three rules:

  • j is strictly greater than i
  • the gap j - i does not exceed k
  • the gap j - i is a prime number

Every time you land on an index you add points[j] to your running total. Your goal is to reach index n - 1 and maximise the total you collect along the way.

Return the highest possible total. If index n - 1 is unreachable under these rules, return null (None in Python).

Notes

  • points may contain negative values. You cannot skip an index you land on.
  • Reaching the last index is required; you may not stop early.

Sub-problem 2: Raise the Weakest Link in a Chain of Services

A processing pipeline consists of n services running in series. Service i starts with a base throughput of throughput[i] and can be scaled up. Scaling service i up by x steps (a non-negative integer) multiplies its throughput by (1 + x) and costs exactly x * cost[i] from your budget.

The overall pipeline throughput is the minimum throughput across all services.

Given a total budget, return the highest overall pipeline throughput achievable without exceeding the budget.

Notes

  • Every cost[i] is a positive integer.
  • You may split the budget across services in any way, including spending nothing.
  • If the pipeline is empty, return 0.

Constraints

  • 0 <= len(points) <= 1000
  • -10^4 <= points[i] <= 10^4
  • 0 <= k <= 1000
  • 0 <= len(throughput) == len(cost) <= 500
  • 1 <= throughput[i] <= 10^5
  • 1 <= cost[i] <= 10^4
  • 0 <= budget <= 10^8

Examples

Example 1

Input: prime_step_and_bottleneck([1, 2, 3, 4, 5], 2, [2, 3], [3, 2], 10)

Output: [9, 6]

Example 2

Input: prime_step_and_bottleneck([5, -10, 20], 2, [5, 4, 6], [2, 3, 1], 0)

Output: [25, 4]

Example 3

Input: prime_step_and_bottleneck([7], 0, [3], [5], 20)

Output: [7, 15]

Hints

Hint 1

For sub-problem 1, generate all primes up to min(k, n-1) once with a sieve, then sweep left to right: the best score at index i is points[i] plus the best reachable score among all predecessors i-p for each prime p. Track unreachability with None, not 0 or -inf.

Hint 2

For sub-problem 2, notice that if a target throughput T is affordable it stays affordable for any smaller target — this monotone property means you can binary search on T directly. For each candidate T, greedily compute the cheapest way to lift every service to at least T and check whether the total cost fits the budget.

Hint 3

For the binary search bounds in sub-problem 2: the lower bound is the current minimum throughput (zero spending needed), and a tight upper bound is the minimum over all services of throughput[i] * (1 + budget // cost[i]) — no single service can exceed that even if it received the entire budget.