NewWork mode: your Claude Code or Codex on work calls, answering from your project's codeSkip to content
Senior1 view

In the RAG pipeline for GigaChat, a long document is first tokenized and then split into chunks of a fixed length. There are 5 standard chunk sizes available: 1, 3, 6, 10, and 15 tokens. What is the minimum number of chunks into which a document of n tokens can be split? ```python def min_context_chunks(n_tokens: int) -> int: ```

Practice

Asked at

AI answer

def min_context_chunks(n_tokens: int) -> int:
    if n_tokens < 0:
        raise ValueError("n_tokens must be non-negative")

    sizes = (1, 3, 6, 10, 15)
    # dp[i] — the minimum number of chunks for a document with i tokens.
    dp = [0] + [n_tokens + 1] * n_tokens

    for i in range(1, n_tokens + 1):
        dp[i] = 1 + min(dp[i - size] for size in sizes if size <= i)

    return dp[n_tokens]

Dynamic programming checks all allowed chunk sizes. The complexity is O(5n), and the space complexity is O(n). For a document with 0 tokens, the result is 0.