← Questions
Parse and repair Markdown emphasis markers
0
I was asked this
No reports yet. Sign in to say where you were asked it.
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]:-
renderedis the repaired text with[em]…[/em],[strong]…[/strong]and[del]…[/del]tags. -
well_formedis"true"when no marker had to be removed or added, else"false". -
repairedis 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
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
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
