Check That Markup Tags in a Post Draft Are Properly Nested
Last updated: November 29, 2026
Quick Overview
Given a post draft containing text and simple markup tags like <b>, </b>, and self-closing <br/>, return True if every opening tag is closed by a matching tag in the correct nesting order and every angle bracket forms a well-formed tag.
Meta
November 29, 20260
0
0 solved
Given a post draft containing text and simple markup tags like <b>, </b>, and self-closing <br/>, return True if every opening tag is closed by a matching tag in the correct nesting order and every angle bracket forms a well-formed tag.
Rich text in Facebook and Instagram composers must be validated before rendering, and a frontend onsite may ask for a small tag checker. The interviewer looks for a scanner that extracts tags robustly and a stack that enforces nesting.
What the Interviewer Expects
- Scan for '<' and find the matching '>'
- Classify tags as opening, closing, or self-closing
- Validate tag names (letters only, non-empty)
- Use a stack to match closing tags
- Reject leftovers on the stack at the end
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you support attributes like <a href='...'>?
- How would you auto-fix mismatched tags instead of rejecting them?
- How would you handle a literal '<' written as <?
- What security issues arise if you render unvalidated markup?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Tags:
<name>,</name>,<name/>withnamemade of ASCII letters, case-sensitive. - Any
<without a closing>or with an invalid name makes the draft invalid. - Text outside tags is ignore...
High-Level Design
Walk the string. When we see <, find the next > with str.find; if none, return False. The inside is body = s[i+1:j].
- If
bodystarts with/, it is a closing tag: the name isbody[1:], ...