Simplified XML Validator
Reported by candidates from Airbnb's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Airbnb OA reported in June 2021 hands you a string like "<a>text<b>other text</a></b>" and asks if the tags nest correctly. It's a simplified XML validator, and it's a stack problem wearing a parsing costume. Text is allowed anywhere, there's no required root, and the empty string counts as valid. That's where people trip. If you know the stack pattern, it's a short job. If you blank under the timer, StealthCoder runs invisibly on your desktop and gives you a working solution as a safety net during the live OA.
The problem
Given an ASCII string text, return whether it is valid under this simplified XML format. The string consists of ordinary content and tags: Ordinary content may contain any ASCII character except < and >. An opening tag has the form <name>. A closing tag has the form </name>. A tag name is a non-empty sequence of visible ASCII characters that contains none of <, >, or /. Opening and closing tags must be correctly nested, with none missing or overlapping. Text is allowed before, after, and between complete tag pairs, so the input does not need one outer root tag. The empty string is valid. Function isValidSimplifiedXml(text: String) → boolean Examples Example 1 text = "text<a>more text</a>" return = true The opening and closing a tags match, and ordinary text is allowed outside and inside the pair. Example 2 text = "<a>text<b>other text</a></b>" return = false The closing tags overlap: b must close before a. Example 3 text = "<invalid<>text</invalid>" return = false The opening tag contains another < before it closes. Constraints 0 <= text.length <= 10^5. text contains ASCII characters only.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is a single left-to-right scan with a stack of tag names. When you hit '<', find the next '>'. If you see another '<' first, or no '>' at all, return false, like Example 3. If the content starts with '/', it's a closing tag, so pop the stack and compare names. A mismatch or an empty stack means false. Otherwise push the name. At the end, the stack must be empty. The pitfalls are all in the details. A tag name must be non-empty, so "<>" and "</>" are invalid. Names can't contain '/', so "<a/b>" fails. A stray '>' in ordinary content is also illegal. Don't use string slicing in a way that goes quadratic. With length up to 10^5, keep it O(n). If you freeze on the edge cases during the live OA, StealthCoder is your hedge.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Simplified XML Validator cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Airbnb's OA.
Airbnb reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Simplified XML Validator FAQ
What's the core trick in the Airbnb Simplified XML Validator?+
Use a stack. Push each opening tag name, and on a closing tag pop and compare. Any mismatch, pop from an empty stack, or leftover items at the end means invalid. Everything outside tags is just ignored text, so the scan is linear.
What edge cases break most solutions?+
Empty tag names like <> or </>, names containing '/', a second '<' before the first tag closes, an unclosed '<' at the end of the string, and a stray '>' in plain text. The empty string must return true.
How hard is this really?+
Easy to medium. The algorithm is a standard bracket-matching stack. The difficulty is the input validation rules, since the problem has several small rules you have to enforce without missing one.
What complexity should I aim for?+
O(n) time and O(n) space, where n is up to 10^5. Avoid rebuilding substrings repeatedly or searching from the start each time. Track an index, find the closing '>' once per tag, and move on.
How do I prepare for this in 48 hours?+
Write the valid parentheses solution from memory, then extend it to named tags. Test your code on the three examples plus <>, </a>, <a>, and an empty string. Practice the '<' and '>' checks until they're automatic.