Implement a Streaming Markdown Parser

Last updated: May 21, 2025

Quick Overview

Build a parser that processes markdown tokens as they stream in one character at a time from an LLM response. Handle partial tokens, nested formatting like bold inside italic, code blocks, and links. The parser must produce valid partial output at any interruption point.

Cursor
Coding & Algorithms
Software Engineer
Cursor
May 21, 2025
Software Engineer
Onsite - Coding
Coding & Algorithms
Medium

11

4

3,761 solved


Build a parser that processes markdown tokens as they stream in one character at a time from an LLM response. Handle partial tokens, nested formatting like bold inside italic, code blocks, and links. The parser must produce valid partial output at any interruption point.

Cursor displays LLM responses in real-time as tokens stream in. The markdown parser must handle incomplete input gracefully. For example, when the model has generated '**bol' so far, the parser must recognize that bold formatting has started but not yet closed. This question tests state machine design and incremental parsing.

What the Interviewer Expects
  • Implement a state machine that tracks open formatting contexts
  • Handle nested formatting correctly (bold inside italic, etc.)
  • Produce valid partial HTML/output at any point in the stream
  • Handle code blocks with proper escaping of inner content
  • Manage edge cases like unclosed formatting and escaped characters
Key Topics to Cover
Streaming parsers and state machines
Markdown specification
Incremental parsing
State management for nested contexts
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
  • How would you handle a code block that contains markdown-like syntax?
  • How would you extend this to support streaming LaTeX rendering?
  • What is the performance profile and can it handle very long streams?
Sharpen Your Skills on Codemia

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

Practice DSA Problems
Sample Answer
Problem Analysis

In this problem, we need to implement a streaming markdown parser that can handle inputs character by character and maintain the state of currently open formatting contexts. The key patterns that appl...

Approach
  1. Initialize a stack to track the current formatting states. Each state will represent a type of formatting (e.g., bold, italic, code block).
  2. Define a function to handle incoming characters. For e...

Submit Your Answer
Markdown supported

Related Questions