KatInterview

Build an Annotation Assignment Schedule

Arrays & HashingModel answer
MediumModel answerArrays & Hashingconstructiveschedulingcombinatorics

Last updated

Construct a valid work schedule assigning human reviewers to tasks under minimum-coverage and no-repeat constraints, then extend it to stay balanced in every prefix.

Build an Annotation Assignment Schedule

A review platform tracks three kinds of entities, each identified by a 1-indexed string label:

  • Tasks: "t1", "t2", ..., "t{totalTask}"
  • Models: "m1", "m2", ..., "m{totalModel}"
  • Reviewers: "r1", "r2", ..., "r{totalHuman}"

A single work item is a triplet [task, model, reviewer], meaning that reviewer checks that task's output using that model. The same task can appear in many work items — different reviewers or different models can all work on the same task.

Given four integers totalTask, totalModel, totalHuman, and k, return any list of work items that satisfies all three rules below. If no such list exists, return an empty list.

Rules:

  1. Every reviewer must appear in at least k work items.
  2. A reviewer may not be paired with the same task more than once.
  3. Every task, model, and reviewer label used must fall within the valid ranges above.

Constraints:

  • 1 <= totalTask, totalModel, totalHuman <= 100
  • 0 <= k <= totalTask

Examples

Example 1

totalTask = 3, totalModel = 1, totalHuman = 2, k = 2

One valid output:

[["t1","m1","r1"], ["t2","m1","r2"], ["t2","m1","r1"], ["t3","m1","r2"]]

Reviewer r1 covers tasks t1 and t2; reviewer r2 covers tasks t2 and t3. Each reviewer appears in exactly 2 work items, and no reviewer repeats a task.

Example 2

totalTask = 1, totalModel = 1, totalHuman = 1, k = 1

One valid output:

[["t1","m1","r1"]]

Example 3

totalTask = 2, totalModel = 3, totalHuman = 3, k = 2

One valid output:

[["t1","m1","r1"],["t2","m1","r2"],["t1","m1","r3"],
 ["t2","m1","r1"],["t1","m1","r2"],["t2","m1","r3"]]

Follow-up: Prefix-Balanced Schedule

Return a schedule that is balanced not just in the final result, but at every intermediate point too.

For any x >= 1, the first x items in the schedule form a prefix. The schedule is prefix-balanced when both conditions hold for every prefix:

  1. Task-model balance: for each task, the number of times it has been paired with each model differs by at most 1 across all models (including models with zero uses so far).
  2. Reviewer-model balance: for each reviewer, the number of times they have used each model differs by at most 1 across all models.

Return any valid prefix-balanced schedule, or an empty list if none exists.

Constraints for the follow-up:

  • 1 <= totalTask, totalModel, totalHuman <= 10
  • 0 <= k <= totalTask

Follow-up Example 1

totalTask = 3, totalModel = 2, totalHuman = 4, k = 2

One valid output:

[["t1","m1","r1"],["t1","m2","r2"],["t1","m1","r3"],["t1","m2","r4"],
 ["t2","m2","r1"],["t2","m1","r2"],["t2","m2","r3"],["t2","m1","r4"]]

Follow-up Example 2

totalTask = 1, totalModel = 1, totalHuman = 1, k = 1

One valid output:

[["t1","m1","r1"]]

Follow-up Example 3

totalTask = 3, totalModel = 1, totalHuman = 3, k = 2

One valid output:

[["t1","m1","r1"],["t1","m1","r2"],["t1","m1","r3"],
 ["t2","m1","r1"],["t2","m1","r2"],["t2","m1","r3"]]

Hints

Hint 1

For the basic version, check feasibility first: how many distinct tasks can a single reviewer be paired with? That ceiling determines when returning an empty list is the right answer — and shows that under the stated constraints a solution almost always exists.

Hint 2

To avoid the duplicate-reviewer-task constraint, think about rotating which task each reviewer starts from. If reviewer j (0-indexed) picks tasks j, j+1, j+2, … (mod totalTask), can two reviewers ever land on the same task twice? You don't need more than one model for this part.

Hint 3

For the prefix-balanced follow-up, rephrase the condition per entity: the sequence of models seen by any single reviewer, or used for any single task, must be balanced at every prefix. A sequence is prefix-balanced if and only if it steps through the models cyclically. Now find one formula for the model index that makes both the per-task and per-reviewer sequences cyclic simultaneously.