Tag Validator

HardStacksimulation

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Given a string code, decide whether it is a valid snippet under these rules, and return true or false. 1. The whole code must be exactly **one closed tag**: <TAG_NAME>TAG_CONTENT</TAG_NAME>, where the start and end tag names are identical. Nothing may appear before the start tag or after the end tag. 2. A valid TAG_NAME is 1 to 9 **uppercase** letters A-Z. 3. TAG_CONTENT may hold any characters, nested closed tags (which must themselves be valid), and CDATA sections. Every < inside the content must begin a CDATA section, a start tag with a valid name, or the end tag that closes the innermost open tag. So an unmatched <, a mismatched end tag, or a tag with an invalid name makes the code invalid. A lone > is ordinary text. 4. A CDATA section starts with <![CDATA[ and ends at the **first** following ]]>. Everything between is plain text and is not parsed, even if it looks like a tag. CDATA may only appear inside tag content.

Examples

Input: code = "<DIV>This is the first line <![CDATA[<div>]]></DIV>"
Output: true
DIV wraps the content, and the `<div>` inside the CDATA section is plain text.
Input: code = "<A> <B> </A> </B>"
Output: false
`</A>` arrives while B is still the innermost open tag.
Input: code = "<DIV> unmatched < </DIV>"
Output: false
The `<` in the content does not start a CDATA section or a valid tag.

Constraints

  • 1 <= code.length <= 500
  • `code` contains English letters, digits, '<', '>', '/', '!', '[', ']', '.' and ' '

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More simulation problems