FastPrepParse, Validate, and Repair Markdown Formatting

Parse, Validate, and Repair Markdown Formatting

Figma logoFigma● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Process a restricted Markdown string with three formatting markers:

  • * for italic text, rendered as [em]...[/em].
  • ** for bold text, rendered as [strong]...[/strong].
  • ~~ for strikethrough text, rendered as [del]...[/del].

Require properly nested formatting. Recognize two-character markers before a single *. A run of *** closes whichever of nested bold and italic formatting is currently on top, then closes the other with the remaining marker. Repair invalid input deterministically:

  1. If the recognized marker matches the top of the open-marker stack, it closes that format.
  2. If the same marker is open deeper in the stack, the current crossing marker is removed.
  3. Otherwise the marker opens a new format.
  4. After the scan, append closing markers for every still-open format from innermost to outermost.

Return three strings: the tagged text rendered from the repaired Markdown, "true" exactly when no marker was removed or appended, and the repaired Markdown.

Function

processMarkdown(text: String) → String[]

Examples

Example 1

text = "I *really* like this"return = ["I [em]really[/em] like this","true","I *really* like this"]

The italic markers are already balanced and properly nested.

Example 2

text = "**Bold and *italic*** plus ~~old~~"return = ["[strong]Bold and [em]italic[/em][/strong] plus [del]old[/del]","true","**Bold and *italic*** plus ~~old~~"]

The italic section is nested inside bold formatting, and the trailing three stars close italic before bold.

Example 3

text = "*one **two* three**"return = ["[em]one [strong]two three[/strong][/em]","false","*one **two three***"]

The crossing italic marker is removed. Bold then closes normally, and the missing outer italic closer is appended.

Constraints

  • 0 <= text.length <= 200000.
  • The input contains no HTML-sensitive characters <, >, or &.
  • Every * and ~ belongs to a recognized marker; literal marker escaping and empty formatted spans are outside this exercise.
  • Under the repair rules, at most the three distinct formatting kinds can be open at once.

More Figma problems

See Figma hiring insights
public String[] processMarkdown(String text) {
    // Write your code here.
}
text"I *really* like this"
expected["I [em]really[/em] like this", "true", "I *really* like this"]
Checking account…