KatInterview

Maximize Throughput in a Serial Manufacturing Pipeline

Stacks & QueuesModel answer
MediumModel answerStacks & Queuessimulationschedulingqueues

Last updated

Design and simulate a fixed-time serial pipeline to maximize completed units, given per-stage processing times, inter-stage buffer capacities, and blocking-after-service rules.

Serial Pipeline Throughput Simulation

Context

You are building a simulation engine for a turn-based factory game. The factory produces widgets through a series of stages that run in a fixed order. Your job is to figure out the best way to operate the line and, when upgrades are available, where to spend them.

Model

  • There are m stages numbered 0 through m-1, run in strict series.
  • Stage i takes exactly p[i] seconds to process one widget. Each stage handles one widget at a time.
  • Between stage i and stage i+1 there is a FIFO queue with a maximum capacity of b[i] widgets.
  • Blocking rule: when a stage finishes a widget but the downstream queue is full, the widget cannot leave. The stage stays blocked — it cannot accept new work — until a slot opens downstream.
  • Raw materials are always available at stage 0.
  • A widget counts as complete only if it exits stage m-1 at or before time T.

Tasks

  1. Explain your throughput strategy. How many widgets can the line produce in window T? What limits it, and why do buffers matter only in certain conditions?

  2. Implement a simulator. Given p, b, and T, return the number of completed widgets.

  3. Implement a greedy upgrade planner. Given a budget of tokens upgrade credits, each of which multiplies one chosen stage's processing time by a factor speedup < 1, allocate credits to maximize completed widgets. Use the simulator to evaluate each candidate upgrade.

What is expected

  • State your assumptions and define the model concretely before writing code.
  • Derive or state the closed-form count for the deterministic case and verify your simulator against it.
  • Discuss when buffers matter and when they do not.
  • Provide working Python code with correct handling of edge cases.
  • Analyze time and space complexity.

Constraints

  • 1 ≤ m ≤ 20
  • p[i] > 0 (floats allowed)
  • b[i] ≥ 0; a value of 0 means no intermediate storage (pure blocking line)
  • T > 0
  • 1 ≤ tokens ≤ 10; 0 < speedup < 1

Hints

Hint 1

The bottleneck — the stage with the largest p[i] — sets the steady-state rate once the pipeline is full. The first widget pays the full sum of all processing times before it exits. Use those two facts to write a closed-form formula and sanity-check your simulator against it.

Hint 2

Model each widget's journey with two recurrences: a start time (the max of when the widget arrived and when the stage last freed up) and a departure time (the max of finish time and when there is room downstream). The downstream-room constraint is what encodes the blocking rule.

Hint 3

For the upgrade planner, greedily try removing one token's worth of time from each stage, simulate the result, and pick the stage with the highest gain. Re-run the same loop after each upgrade, because the bottleneck can shift. When no stage improves output, break ties by upgrading whichever stage is currently slowest.