Split a Long Document Into Retrieval Chunks Minimizing the Largest

Last updated: November 29, 2026

Quick Overview

Given the token counts of a document's paragraphs in order and a maximum number of chunks, group consecutive paragraphs into at most that many chunks so the largest chunk has as few tokens as possible, never splitting a paragraph.

Harvey
Coding & Algorithms
Software Engineer
Harvey
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Given the token counts of a document's paragraphs in order and a maximum number of chunks, group consecutive paragraphs into at most that many chunks so the largest chunk has as few tokens as possible, never splitting a paragraph.

Harvey ingests very long contracts and filings into retrieval indexes and model context windows, so chunking with a token budget is a real concern. The interviewer is testing whether you spot binary search on the answer with a greedy feasibility check, and whether you can reconstruct the chunks.

What the Interviewer Expects
  • Recognize the monotonic feasibility property
  • Set bounds at max paragraph and total tokens
  • Write a greedy check counting chunks for a given limit
  • Reconstruct the actual chunk boundaries
  • Compare with the O(n^2 k) DP
Key Topics to Cover
binary search on answer
greedy
partitioning
chunking
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • What if chunks should also prefer breaking at section headings?
  • How would you add overlap between chunks for retrieval?
  • What if a single paragraph exceeds the model's context limit?
  • How would you balance chunk sizes rather than just minimizing the maximum?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: list of positive integers (tokens per paragraph) and max_chunks. Output the minimal possible largest chunk size and one valid split as lists of paragraph indices. Paragraphs stay in order and...

High-Level Design

If a limit L is achievable with at most k chunks, any larger limit is too. So binary search L between max(tokens) (a chunk must hold the biggest paragraph) and sum(tokens) (one chunk). Feasibility...


Submit Your Answer
Markdown supported

Related Questions