Validate a code snippet of nested tags and CDATA sections

Quick Overview

Implement a validator that decides whether a code snippet made of nested tags and CDATA sections follows a strict set of parsing rules. It tests careful single-pass string scanning with a stack of open tags, the order in which overlapping cases are checked, and handling of unmatched brackets and invalid tag names.

Validate a code snippet of nested tags and CDATA sections

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

Given a string `code` representing a code snippet, implement a tag validator that parses the code and returns whether it is valid. ### Function Signature ```python def is_valid(code: str) -> bool: ``` ### Rules A code snippet is valid if all of the following rules hold. - The whole snippet must be wrapped in one valid closed tag. Text before the first tag, text after the outer end tag, or two top-level tags side by side make the snippet invalid. - A closed tag (not necessarily valid) has exactly the form `<TAG_NAME>TAG_CONTENT</TAG_NAME>`, where `<TAG_NAME>` is the start tag and `</TAG_NAME>` is the end tag. The `TAG_NAME` in the start and end tags must be the same. A closed tag is valid only if its `TAG_NAME` and its `TAG_CONTENT` are both valid. - A valid `TAG_NAME` contains only upper-case letters `A` to `Z` and has length 1 to 9. - A valid `TAG_CONTENT` may contain other valid closed tags, cdata sections and any other characters, except an unmatched `<`, an unmatched start or end tag, or a tag (matched or not) whose `TAG_NAME` is invalid. - A start tag is unmatched if no end tag with the same `TAG_NAME` closes it, and an end tag is unmatched if no start tag with the same name opened it. Tags must also nest properly, so `<A><B></A></B>` is invalid. - A `<` is unmatched if no `>` comes after it. Whenever you find `<` or `</`, all the characters up to the next `>` are parsed as a `TAG_NAME`, which may then turn out to be invalid. - A `>` that does not close a tag or cdata section is ordinary text. - A cdata section has the form `<![CDATA[CDATA_CONTENT]]>`. `CDATA_CONTENT` is the characters between `<![CDATA[` and the first `]]>` after it. - `CDATA_CONTENT` may contain any characters. The validator does not parse it, so anything inside it that looks like a tag, valid or invalid, is treated as regular characters. ### Constraints - `1 <= len(code) <= 500` - `code` consists of English letters, digits, `'<'`, `'>'`, `'/'`, `'!'`, `'['`, `']'`, `'.'` and `' '`. ### Examples ```text Input: code = "<DIV>This is the first line <![CDATA[<div>]]></DIV>" Output: True ``` The snippet is wrapped in `<DIV>...</DIV>`, and `DIV` is a valid name (upper-case, length 3). The content is plain text plus a cdata section whose content `<div>` is not parsed. ```text Input: code = "<DIV>>> ![cdata[]] <![CDATA[<div>]>]]>]]>>]</DIV>" Output: True ``` The cdata section starts at `<![CDATA[` and ends at the first `]]>`, so its content is `<div>]>`. The stray `>` characters, the lower-case `![cdata[]]` (it has no `<`) and the `]]>>]` after the section are all plain text. ```text Input: code = "<DIV> unmatched < </DIV>" Output: False ``` The `<` after "unmatched" starts a tag name that runs to the next `>`, so the name is ` </DIV` with its leading spaces. That name is not made of upper-case letters, so the snippet is invalid.

Overview: Implement a validator that decides whether a code snippet made of nested tags and CDATA sections follows a strict set of parsing rules. It tests careful single-pass string scanning with a stack of open tags, the order in which overlapping cases are checked, and handling of unmatched brackets and invalid tag names.

General logo
General
Nov 25, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Given a string code representing a code snippet, implement a tag validator that parses the code and returns whether it is valid.

Function Signature

def is_valid(code: str) -> bool:

Rules

A code snippet is valid if all of the following rules hold.

  • The whole snippet must be wrapped in one valid closed tag. Text before the first tag, text after the outer end tag, or two top-level tags side by side make the snippet invalid.
  • A closed tag (not necessarily valid) has exactly the form <TAG_NAME>TAG_CONTENT</TAG_NAME> , where <TAG_NAME> is the start tag and </TAG_NAME> is the end tag. The TAG_NAME in the start and end tags must be the same. A closed tag is valid only if its TAG_NAME and its TAG_CONTENT are both valid.
  • A valid TAG_NAME contains only upper-case letters A to Z and has length 1 to 9.
  • A valid TAG_CONTENT may contain other valid closed tags, cdata sections and any other characters, except an unmatched < , an unmatched start or end tag, or a tag (matched or not) whose TAG_NAME is invalid.
  • A start tag is unmatched if no end tag with the same TAG_NAME closes it, and an end tag is unmatched if no start tag with the same name opened it. Tags must also nest properly, so <A><B></A></B> is invalid.
  • A < is unmatched if no > comes after it. Whenever you find < or </ , all the characters up to the next > are parsed as a TAG_NAME , which may then turn out to be invalid.
  • A > that does not close a tag or cdata section is ordinary text.
  • A cdata section has the form <![CDATA[CDATA_CONTENT]]> . CDATA_CONTENT is the characters between <![CDATA[ and the first ]]> after it.
  • CDATA_CONTENT may contain any characters. The validator does not parse it, so anything inside it that looks like a tag, valid or invalid, is treated as regular characters.

Constraints

  • 1 <= len(code) <= 500
  • code consists of English letters, digits, '<' , '>' , '/' , '!' , '[' , ']' , '.' and ' ' .

Examples

Input: code = "<DIV>This is the first line <![CDATA[<div>]]></DIV>"
Output: True

The snippet is wrapped in <DIV>...</DIV>, and DIV is a valid name (upper-case, length 3). The content is plain text plus a cdata section whose content <div> is not parsed.

Input: code = "<DIV>>>  ![cdata[]] <![CDATA[<div>]>]]>]]>>]</DIV>"
Output: True

The cdata section starts at <![CDATA[ and ends at the first ]]>, so its content is <div>]>. The stray > characters, the lower-case ![cdata[]] (it has no <) and the ]]>>] after the section are all plain text.

Input: code = "<DIV>  unmatched <  </DIV>"
Output: False

The < after "unmatched" starts a tag name that runs to the next >, so the name is </DIV with its leading spaces. That name is not made of upper-case letters, so the snippet is invalid.