KatInterview

Stream Token Lists into Fixed-Size Blocks with a Validity Mask

Arrays & HashingAuto-graded · 21 test cases
MediumAuto-gradedArrays & Hashingarrayssimulationtwo-pointers

Last updated

Concatenate truncated token lists into equal-length blocks, zero-pad the last one, and build a parallel 0/1 mask marking real tokens versus padding.

Fixed-Size Block Packing with a Mask

A training pipeline feeds token sequences of different lengths to a model. To avoid wasting compute on padding, the sequences are joined end to end and cut into blocks that all have the same length.

Write a function that takes a list of token sequences and a block length, and returns the packed blocks together with a mask for each block.

Rules

  1. Process the sequences in the given order.
  2. Skip any empty sequence.
  3. If a sequence has more than block_len tokens, keep only its first block_len tokens and discard the rest. This truncation is applied to each sequence individually, before it is joined to the stream.
  4. Append the remaining tokens, in order, to one continuous stream. A block is filled completely before the next block is started. A sequence is not kept whole: if it does not fit in the space left in the current block, its tokens continue at the start of the next block.
  5. Every block has exactly block_len entries. If the final block is not full once all tokens have been placed, fill the rest of it with the padding value 0.
  6. Never produce a block that consists only of padding. If there are no tokens at all, there are no blocks.
  7. For each block, produce a mask of the same length: 1 at positions holding a real token and 0 at padding positions. The mask depends on position only, not on token value, so a real token whose value is 0 still gets mask 1.

Input

  • sequences: a list of lists of integers.
  • block_len: a positive integer, the length of every block.

Output

Return a two-element list [blocks, masks], where:

  • blocks is a list of blocks, each a list of block_len integers.
  • masks is a list of masks, each a list of block_len values from {0, 1}, with masks[i] describing blocks[i].

If nothing is packed, return [[], []].

Constraints

  • block_len >= 1
  • Each sequence is a (possibly empty) list of integers.
  • Token order must be preserved exactly.
  • The return value must be a list (not a tuple) of two lists.

Examples

Example 1

Input: pack_into_blocks([[1, 2], [3, 4, 5], [6]], 4)

Output: [[[1, 2, 3, 4], [5, 6, 0, 0]], [[1, 1, 1, 1], [1, 1, 0, 0]]]

Example 2

Input: pack_into_blocks([[1, 2, 3, 4, 5]], 3)

Output: [[[1, 2, 3]], [[1, 1, 1]]]

Example 3

Input: pack_into_blocks([[], [7], [], [8, 9]], 4)

Output: [[[7, 8, 9, 0]], [[1, 1, 1, 0]]]

Hints

Hint 1

Think of the input as one long stream of tokens that you pour into a buffer of size block_len, rather than as items you must keep together.

Hint 2

Truncate each sequence to its first block_len tokens before streaming it. Skip empty ones.

Hint 3

Build the mask in step with the tokens (append 1 for each real token) instead of deriving it from token != 0. When the final buffer is non-empty, pad both the tokens and the mask with zeros.