Katkat

Count Completed Jobs in a Serial Single-Worker Pipeline

PlaidSoftware Engineer · Technical Screen

Last updated

Determine the total number of jobs completed by a single worker executing a sequence of stage-gated tasks before a time limit expires. Tests linear simulation, division/modulo arithmetic, handling of zero-duration and zero-count edge cases, and 64-bit integer safety.

Problem Statement

You are monitoring an automated data ingestion pipeline executed by a single worker. The pipeline consists of n sequential stages numbered from 0 to n - 1. The worker must process each stage in order: it cannot begin working on jobs in stage i + 1 until all jobs in stage i have been completed.

You are given two integer arrays of length n:

  • counts[i]: the number of identical jobs in stage i.
  • durations[i]: the time required to complete a single job in stage i.

You are also given an integer time_limit representing the total processing time available to the worker. If the worker starts a job, it only counts as completed if the entire duration for that job finishes at or before time_limit. If the time limit expires while a job is in progress, that job is not completed, and the worker stops immediately.

Return the total number of completed jobs across all stages within time_limit. Do not mutate the input arrays.

Function Signature

def count_completed_jobs(counts: list[int], durations: list[int], time_limit: int) -> int:
    pass

Examples

Example 1

Input: counts = [3, 2, 5], durations = [4, 10, 2], time_limit = 25
Output: 4
Explanation:
- Stage 0: 3 jobs * 4 units/job = 12 units. All 3 jobs complete. Remaining time: 25 - 12 = 13.
- Stage 1: Each job takes 10 units. With 13 units left, the worker can finish floor(13 / 10) = 1 job, using 10 units. Remaining time: 3.
- The worker attempts the second job of Stage 1, which requires 10 units, but only 3 units remain. The job cannot finish, and Stage 1 is not fully completed, so Stage 2 is never reached.
Total completed jobs = 3 + 1 = 4.

Example 2

Input: counts = [0, 4, 2], durations = [5, 0, 3], time_limit = 4
Output: 5
Explanation:
- Stage 0: Has 0 jobs, so it completes immediately using 0 units. Remaining time: 4.
- Stage 1: 4 jobs taking 0 units each. All 4 complete instantly. Remaining time: 4.
- Stage 2: 2 jobs taking 3 units each. The worker can complete floor(4 / 3) = 1 job, using 3 units. Remaining time: 1.
Total completed jobs = 0 + 4 + 1 = 5.

Constraints

  • n == counts.length == durations.length
  • 1 <= n <= 10^5
  • 0 <= counts[i] <= 10^9
  • 0 <= durations[i] <= 10^9
  • 0 <= time_limit <= 10^18
  • Note: In languages with fixed-width integers (e.g., C++, Java), intermediate products and totals can exceed standard 32-bit integer ranges; use 64-bit integer types (long long / long).

Hints

Hint 1

Process stages sequentially in O(1) time per stage by using integer division rather than simulating job-by-job.

Hint 2

Pay special attention to stages where durations[i] == 0: all jobs in that stage complete instantly without consuming any remaining time.

Hint 3

If the worker cannot complete all jobs in the current stage, compute how many partial jobs finish, add them to the total, and break early since the pipeline cannot advance.

Follow-up questions

  • How would you adapt this if each stage also had a fixed transition/setup overhead that must be paid before any job in that stage can start?
  • If we had `k` parallel workers assigned to the current active stage instead of a single worker, how would the completion calculation change?
  • Suppose we want to answer `q` queries of different `time_limit` values efficiently on an immutable pipeline. How would you precompute the stages to answer each query in O(log n) time?