Build an Annotation Assignment Schedule
Arrays & HashingModel answerLast 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:
- Every reviewer must appear in at least
kwork items. - A reviewer may not be paired with the same task more than once.
- Every task, model, and reviewer label used must fall within the valid ranges above.
Constraints:
1 <= totalTask, totalModel, totalHuman <= 1000 <= 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:
- 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).
- 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 <= 100 <= 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.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Part 1 — Basic Schedule
Check feasibility first. A reviewer can appear with at most totalTask distinct tasks (rule 2). Since rule 1 demands at least k appearances, the only infeasible case is k > totalTask. Everything else has a solution.
Construction. Because models are unconstrained, m1 works throughout. Give reviewer j (0-indexed) tasks j, j+1, ..., j+k-1 taken modulo totalTask. Since k <= totalTask, those k residues are all distinct, so no reviewer repeats a task. Every reviewer gets exactly k work items. All IDs stay in range by construction.
def build_schedule(totalTask, totalModel, totalHuman, k):
if k > totalTask:
return []
result = []
for j in range(totalHuman):
for i in range(k):
task_idx = (j + i) % totalTask
result.append([f"t{task_idx + 1}", "m1", f"r{j + 1}"])
return result
Complexity: O(H · k) time and space — exactly the output size, so optimal.
Part 2 — Prefix-Balanced Schedule
Reframe the condition. "Balanced at every prefix" sounds like a global constraint, but it decomposes per entity. Look at the subsequence of models used by a single reviewer, or assigned to a single task, in schedule order. A prefix of the full schedule induces a prefix of that subsequence. The balance rule therefore says:
Every prefix of every per-reviewer and per-task model subsequence must have per-model counts differing by at most 1.
A sequence satisfies this for every prefix if and only if it is cyclic: s, s+1, s+2, ... (mod M). After n steps each model appears either floor(n/M) or ceil(n/M) times — the tightest possible spread.
The key insight. Arrange the schedule in rounds. Round i (0-indexed, i from 0 to k-1) assigns task t_{i+1} to every reviewer in order r1, r2, ..., r_H. Choose the model for reviewer j in round i as:
model_index = (i + j) mod M
- Per-task balance. Task
iappears only in roundias one contiguous block. Its models form the sequencei, i+1, ..., i+H-1 (mod M)— a cyclic walk starting ati. Any prefix of the global schedule that falls mid-block contributes a prefix of that walk, which is balanced. - Per-reviewer balance. Reviewer
jappears once per round, in rounds0, 1, ..., k-1. Its model sequence isj, j+1, ..., j+k-1 (mod M)— also a cyclic walk. Balanced at every prefix.
The i + j formula is what makes both families cyclic simultaneously: incrementing i advances each reviewer's cycle, and incrementing j advances the current task's cycle.
This construction also satisfies the basic rules: each reviewer covers tasks t1 through tk, all distinct (since k <= totalTask), and gets exactly k work items.
def build_balanced_schedule(totalTask, totalModel, totalHuman, k):
if k > totalTask:
return []
result = []
for i in range(k): # round i -> task t_{i+1}
for j in range(totalHuman): # each reviewer in order
model_idx = (i + j) % totalModel
result.append([f"t{i + 1}", f"m{model_idx + 1}", f"r{j + 1}"])
return result
Complexity: O(H · k) time and space. The small follow-up constraints hint at backtracking, but this construction needs none.
Verification Against Examples
Follow-up Example 1 (T=3, M=2, H=4, k=2):
- Round 0 (task
t1): models forr1..r4are(0+0)%2=0, (0+1)%2=1, (0+2)%2=0, (0+3)%2=1→m1,m2,m1,m2. - Round 1 (task
t2):1%2=1, 2%2=0, 3%2=1, 4%2=0→m2,m1,m2,m1.
This matches the expected output exactly.
Edge Cases
| Scenario | Behaviour |
|---|---|
k = 0 | Return []; vacuously valid in both parts |
M = 1 | All model expressions resolve to m1; balance is trivial |
k = totalTask | Every reviewer labels every task; all tasks are still distinct per reviewer |
k > totalTask | Return [] |
Common Mistakes
Using backtracking or greedy-with-repair. It can scrape by under the small follow-up constraints but is hard to prove correct and easy to break on edge cases. The construction is O(H · k) and provably correct.
Balancing only the final counts. The follow-up explicitly requires every prefix, not just the end result. A round-robin assignment that looks balanced at the end can be wildly unbalanced midway through.
Using j mod M alone for the model. That keeps tasks balanced but means reviewer j uses the same model in every round — a constant sequence — which breaks reviewer-model balance when M > 1.
Forgetting the empty-list case. When k = 0, the empty list is valid. Emitting any work items is unnecessary and wastes time.