KatInterview

Plain-Text to HTML: Paragraphs, Blockquotes, and Strikethrough

StringsModel answer
MediumModel answerStringsparsingstring-processingtwo-pass

Last updated

Write a function that converts a plain-text string to HTML with paragraphs, soft line breaks, blockquotes, and strikethrough, handling all edge cases correctly.

Plain-Text to HTML: Paragraphs, Blockquotes, and Strikethrough

Write a function render(text: str) -> str that turns a plain-text string into an HTML string. Support exactly the four features below and nothing else.

Feature specification

1. Paragraphs One or more blank lines (lines that are empty or contain only whitespace) act as paragraph separators. Wrap each paragraph in <p>…</p>. Never emit an empty <p></p> for leading, trailing, or consecutive blank lines.

2. Soft line breaks A single newline between two non-blank lines within the same paragraph becomes <br/> in the output.

3. Blockquotes A run of consecutive lines where every line begins with "> " (a > immediately followed by a space, at column 0) forms one <blockquote>…</blockquote>. Before rendering, strip the leading "> " from each such line. Soft line breaks inside a blockquote also become <br/>. A blank line ends the blockquote. Blockquotes are siblings of <p> elements — never nested inside one. A line like ">no space" or " > x" is plain text, not a blockquote line.

4. Strikethrough A matched pair of ~~ markers wrapping non-empty content becomes <del>…</del>. For example: ~~like this~~ → <del>like this</del>. An opener with no matching closer in the same block stays as literal text. An empty pair ~~~~ also stays literal.

5. Inline markup is block-scoped A ~~ that opens in one block and never closes within that same block is treated as plain text. Markup cannot cross paragraph or blockquote boundaries.

The output must be valid HTML. HTML special characters in the input (<, >, &) must be escaped.

What your answer should cover

  • Your overall strategy: how you divide the problem into block-level and inline-level phases.
  • How you group lines into blocks in a single linear scan.
  • How you process inline markup within each block.
  • Time and space complexity in terms of input length n.
  • How you handle each of the following edge cases:
    • The empty string and strings that contain only newlines.
    • Trailing whitespace on content lines.
    • Whitespace-only lines acting as paragraph separators.
    • A run of lines that alternates between quoted and plain lines.
    • Two blockquote groups separated by a blank line.
    • A ">..." line missing the required space after >.
    • An unmatched or empty ~~ pair.
    • A ~~ pair that would need to span a paragraph boundary.
    • Input containing <, >, or &.

Example input

This is a paragraph with a
soft line break.

> Some quoted text
> continues here

This has a ~~strikethrough~~ word.

Expected output

<p>This is a paragraph with a<br/>soft line break.</p>
<blockquote>Some quoted text<br/>continues here</blockquote>
<p>This has a <del>strikethrough</del> word.</p>

Include at least five test cases that exercise the edge cases listed above.

Hints

Hint 1

Separate the work into two independent phases: a line-level pass that groups lines into typed blocks (paragraph or blockquote), and a character-level pass that applies inline markup within each block. This separation makes the 'no cross-block markup' rule free — the inline scanner never sees a block boundary.

Hint 2

During the line scan, maintain a current block kind and a line buffer. Three events should flush the buffer and emit an element: a blank line, a change in line kind (e.g., a quoted line appears after a plain one), and end of input. Flush before switching kinds so you never mix content.

Hint 3

For the strikethrough scan, walk left to right through the block text. Each time you find an opener `~~`, immediately search forward for the next `~~` as the closer. If no closer exists in the current block, emit everything from the opener to the end as literal text and stop. This greedy left-to-right pairing handles consecutive pairs like `~~a~~ ~~b~~` correctly and runs in O(n).