FastPrepFastPrep
Problem Brief

Markdown Header Chunks

FULLTIMEPHONE SCREEN

Split a Markdown document into chunks whose text length does not exceed maxChunkSize whenever possible. Lines must remain intact.

When a new chunk starts inside a Markdown header section, prepend the active header path to that chunk. For example, if the current content is under # A and ## B, the new chunk should start with those header lines before adding the next content lines.

Return each chunk as a display string where the lines inside that chunk are joined by | .

1Example 1

Input
markdown = "# A\nshort\nlonger line\n## B\nx\ny", maxChunkSize = 20
Output
["# A | short","# A | longer line","# A | ## B | x | y"]
Explanation

The second chunk repeats the active # A header before continuing content under that section.

2Example 2

Input
markdown = "# Guide\nalpha\nbeta", maxChunkSize = 30
Output
["# Guide | alpha | beta"]
Explanation

The whole document fits within one chunk.

Constraints

Limits and guarantees your solution can rely on.

Header lines are lines whose first non-space character is #. If a single non-header line plus its active headers is longer than maxChunkSize, keep that line in its own chunk with the active headers.

public String[] chunkMarkdown(String markdown, int maxChunkSize) {
    // write your code here
}
Input

markdown

"# A\nshort\nlonger line\n## B\nx\ny"

maxChunkSize

20

Output

["# A | short", "# A | longer line", "# A | ## B | x | y"]

Sign in to submit your solution.