← Questions

Parse and repair Markdown emphasis markers

0
FigmaCodingLast asked Jun 2024

A coding interview question asked in Figma's coding round, last asked Jun 2024. It tests parsing, stacks and strings.

Process a small subset of Markdown with three markers: * for italic, ** for bold and ~~ for strikethrough. Formats must nest properly. Render the text, say whether it was well formed, and repair it when it was not.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • process(text: str) -> list[str] — returns [rendered, well_formed, repaired]:
    • rendered is the repaired text with [em]…[/em], [strong]…[/strong] and [del]…[/del] tags.
    • well_formed is "true" when no marker had to be removed or added, else "false".
    • repaired is the repaired Markdown.

Rules:

  • Recognize ** and ~~ before a single *. A run of *** closes whichever of bold and italic is innermost, then closes the other with the remaining star.
  • Keep a stack of open formats. A marker that matches the top of the stack closes it. A marker of a format open deeper in the stack would cross formats, so it is removed. Any other marker opens a new format.
  • At the end, close every format still open, innermost first.

Example

process("I *really* like this")
->  ["I [em]really[/em] like this", "true", "I *really* like this"]

process("**Bold and *italic*** plus ~~old~~")
->  ["[strong]Bold and [em]italic[/em][/strong] plus [del]old[/del]", "true",
     "**Bold and *italic*** plus ~~old~~"]

process("*one **two* three**")
->  ["[em]one [strong]two three[/strong][/em]", "false", "*one **two three***"]
# the * after "two" would cross the open bold, so it is removed; the italic is closed at the end

Constraints

  • Up to 200,000 characters; no <, > or &.
  • Every * and ~ belongs to a marker; escaping and empty spans are out of scope.

Clarifying questions

  • How is *** read when nothing is open: bold then italic, or italic then bold?
  • Should crossing markers be removed or should the formats be split?
  • Are escaped markers (\*) possible?
  • Is the output tag format fixed?

What a strong answer covers

  • A single left-to-right scan that tokenizes markers (two-character markers first) and keeps a stack of open formats.
  • The three repair rules applied in a fixed order, and closers appended at the end innermost first.
  • Building the repaired Markdown and the rendered output in the same pass (lists of pieces joined at the end), O(n) time.
  • Tests: proper nesting, crossing markers, *** both ways, unclosed formats, text with no markers.

Follow-ups

  • Add inline code with backticks, inside which markers are literal.
  • Support escaping with a backslash.
  • Repair crossing markers by closing and reopening the inner format instead of dropping the marker.

Where it's asked

  • Figma · Coding · Last asked Jun 202460 min · One or two pair-programming rounds against provided tests

What it tests

parsing · stacks · strings

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding