The size limit part was fine, pretty standard sliding window type thinking.
Clarify the requirements first: chunk size limit, whether to split on paragraph/section boundaries, and how to handle code blocks or lists. Then propose a two-pass algorithm: first parse the markdown into a tree of sections with headers, then traverse and split content while tracking the active header stack. Finally, discuss trade-offs like chunk size vs. semantic coherence and edge cases like headers inside code blocks.
Pro tip: Mention that you'd use a proper markdown parser (e.g., markdown-it or remark) to avoid regex pitfalls, and that you'd write tests for edge cases like nested headers, headers in code blocks, and very long sections.
Ask about maximum chunk size, whether chunks should be split at paragraph or section boundaries, and how to handle code blocks, lists, and tables. Confirm that headers should be repeated in each chunk that continues a section.
Use a markdown parser to build an AST or a list of tokens. Extract headers and their hierarchy to maintain a stack of active headers as you traverse the document.
Iterate through the parsed content, accumulating text until the size limit is reached. When splitting, start the new chunk with the current header stack (formatted as markdown headers) followed by the remaining content.
Consider headers inside code blocks (should not be treated as headers), very long sections that exceed the limit without a natural break, and ensuring that repeated headers don't push the chunk over the limit. Write tests for these scenarios.
Talk about the trade-off between strict size limits and semantic coherence (e.g., avoiding splitting mid-sentence). Mention possible optimizations like pre-computing header positions or using a streaming approach for large files.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.