Parse Nested HTML into a Tree
Reported by candidates from Apple's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Apple reported this one in October 2026, and the input size is the first thing to read. With html.length up to 200000, anything that re-scans the string per tag or builds the tree through repeated substring copying will crawl. The task is a tree parse with a preorder output, and it's friendlier than it looks. If you've got the OA in a day or two, the real question is whether you can write the scan cleanly under pressure. StealthCoder sits invisibly on your screen as a safety net if your mind goes blank mid-assessment, but the idea here is short enough to own before you start.
The problem
Parse a well-formed markup string into its nested element tree and return the element names in depth-first preorder. The grammar contains paired lowercase tags such as <section>...</section> and arbitrary text leaves. Attributes, comments, entities, void elements, and self-closing tags are outside scope. The input has exactly one root element. Function preorderElementNames(html: String) → String[] Examples Example 1 html = "<div>Hello<span>world</span></div>" return = ["div","span"] Preorder visits the root div before its nested span. Example 2 html = "<a><b></b><c><d></d></c></a>" return = ["a","b","c","d"] Depth-first preorder follows a, b, c, then d. Constraints 7 <= html.length <= 200000. Tag names contain one to twenty lowercase ASCII letters. The markup is well formed and has one root element.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: preorder means you record a name the moment you see its opening tag. You don't need to build a tree at all. Scan once with an index. When you hit '<' and the next char isn't '/', read letters until '>' and append that name to the result. When it's '</', skip to '>'. Everything else is text, so skip it. Because the markup is well formed, you don't even need a stack for correctness, though a stack is fine if you want to explain the nesting. That's O(n) time and the output holds at most n/7 names. The common pitfall is building substrings inside a loop or using regex that backtracks, which hurts at 200000 characters. Another miss is mishandling text leaves that sit between tags. If you freeze live, StealthCoder is the hedge, but this scan is about fifteen lines.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Parse Nested HTML into a Tree 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 would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Apple's OA.
Apple reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Parse Nested HTML into a Tree FAQ
How hard is this Apple OA question really?+
Easier than the title suggests. The statement strips out attributes, comments, entities and self-closing tags, so the only work is a linear scan that spots opening tags. Most of the risk is sloppy index handling, not algorithmic difficulty.
What's the trick to solving it?+
Preorder means you emit a name when the opening tag appears. Skip closing tags and text. You never need to materialize the tree. One pass over the string, collecting letters between '<' and '>' when the next character isn't '/'.
Do I need a stack or recursion?+
Not for the output. Well-formed input guarantees opening tags already appear in preorder. A stack or recursion is only useful if you want to build the tree explicitly, and recursion could hit depth limits with 200000 characters of deeply nested tags.
What edge cases should I test?+
Test a single root with only text inside, deep nesting like <a><b><c></c></b></a>, siblings at several levels, and text mixed between child tags. Also test a one-letter tag and a twenty-letter tag to confirm your name parsing doesn't cut off.
How do I prepare for this in 48 hours?+
Write the single-pass scanner by hand twice, then trace both examples on paper. Practice tokenizing '<', '</' and '>' with an index pointer. Spend leftover time on similar tree traversal output problems so preorder feels automatic.