← Questions

Pack fine-tuning samples into fixed-length sequences

0
MicrosoftCodingLast asked Apr 2026

A coding interview question asked in Microsoft's recruiter screen, last asked Apr 2026. It tests llm training, bin packing and data loading.

For supervised fine-tuning, each sample is a prompt and an answer, both already tokenized. Pack samples into sequences of a fixed length so little space is wasted, and return, for every packed sequence, what the training step needs: token ids, which tokens count in the loss, positions that restart for each sample, and where each sample and its answer sit, so samples do not attend to each other.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • pack(samples: list[tuple[list[int], list[int]]], max_len: int, pad_id: int) -> list[dict]
  • Each sample is (prompt_ids, answer_ids); its tokens are prompt_ids + answer_ids.
  • Packing is deterministic: go through samples in order; append a sample to the current sequence if it fits in the space left, otherwise pad the current sequence to max_len and start a new one.
  • A sample longer than max_len is truncated from the end to max_len tokens.
  • Each output dict has input_ids (length max_len), loss_mask (1 on answer tokens, 0 on prompt tokens and padding), position_ids (restart at 0 for each sample, 0 on padding), segments (half-open (start, end) per sample) and answer_spans (half-open (start, end) of each answer).

Example

samples = [([11, 12], [13, 14]),        # 4 tokens
           ([21], [22, 23]),            # 3 tokens
           ([31, 32, 33], [34, 35])]    # 5 tokens, does not fit in the 1 slot left
pack(samples, max_len=8, pad_id=0)  ->
[
  {"input_ids":    [11, 12, 13, 14, 21, 22, 23, 0],
   "loss_mask":    [0, 0, 1, 1, 0, 1, 1, 0],
   "position_ids": [0, 1, 2, 3, 0, 1, 2, 0],
   "segments":     [(0, 4), (4, 7)],
   "answer_spans": [(2, 4), (5, 7)]},
  {"input_ids":    [31, 32, 33, 34, 35, 0, 0, 0],
   "loss_mask":    [0, 0, 0, 1, 1, 0, 0, 0],
   "position_ids": [0, 1, 2, 3, 4, 0, 0, 0],
   "segments":     [(0, 5)],
   "answer_spans": [(3, 5)]},
]

Constraints

  • Up to 10^6 samples; max_len up to 32,768.
  • Output must be identical across runs for the same input.

Clarifying questions

  • Is the loss mask aligned with the input tokens, or with the shifted labels the model predicts?
  • How should over-long samples be truncated: drop them, cut the prompt, or cut the answer?
  • Is sample order fixed, or may I reorder to pack tighter?
  • Does the attention kernel take segment boundaries (variable-length attention) or a full block-diagonal mask?
  • Should an EOS token be appended after each answer?

What a strong answer covers

  • A correct next-fit packer with clean bookkeeping of offsets, so segments and answer spans line up with input_ids.
  • Loss only on answer tokens, never on prompts or padding, and the off-by-one when labels are shifted for next-token prediction.
  • Position ids that restart per sample and a block-diagonal attention mask (or cumulative sequence lengths) so packed samples do not see each other.
  • Edge cases: empty prompt or answer, a sample that exactly fills the remaining space, truncation that cuts into the answer, an empty input list.
  • Packing efficiency: next-fit versus first-fit decreasing (sorting by length), and how reordering affects determinism and curriculum.
  • Tests on hand-built cases that check every field.

Follow-ups

  • Use first-fit decreasing to reduce padding; how much does it save, and what does it cost?
  • Support multi-turn samples where only the assistant turns are trained.
  • Stream samples and pack on the fly with bounded memory.
  • How does packing change the effective batch size and the loss normalization?

Where it's asked

What it tests

llm training · bin packing · data loading

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding