FastPrepParse Nested HTML into a Tree
Problems/Parse Nested HTML into a Tree

Parse Nested HTML into a Tree

Apple logoApple● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The interview report identifies an intentionally vague HTML parser represented as a tree and traversed with depth-first search. This practice bounds the grammar to paired lowercase tags and text leaves and returns element names in preorder. Approximate source match: 66%.

You are given a string html of HTML-like markup. Read it as a tree. Each element is a node, and the elements nested directly inside it are its children. Return the names of all elements in depth-first order, where every element comes before the elements inside it. This order is called preorder.

The markup uses only paired lowercase tags, such as <section>...</section>, plus any text between tags. Text is not an element, so it never shows up in the answer. The input never has attributes, comments, character entities, self-closing tags, or void elements (tags that never get a closing tag). It has exactly one root element, which holds everything else.

Function

preorderElementNames(html: String) → String[]

Examples

Example 1

html = "<div>Hello<span>world</span></div>"return = ["div","span"]

The root is div, so it comes first. Inside it are the text Hello and the element span. Text is skipped, so the answer is div, then span.

Example 2

html = "<a><b></b><c><d></d></c></a>"return = ["a","b","c","d"]

The root a has two children, b and c, and c holds d. Preorder visits a, then b. Next it visits c and, before moving on, the d inside it. The answer is a, b, c, 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.

More Apple Onsite Interview questions

  • FIFO Mutex Operation TraceONSITE INTERVIEW · Seen Oct 2026
  • Implement Hit CounterPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Topological OrderingPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Thread-Safe Concurrent Linked ListONSITE INTERVIEW · Seen Sep 2026
  • Palindrome Match PercentageONSITE INTERVIEW · Seen Sep 2026
  • Binary Tree Nodes at Distance KONSITE INTERVIEW · Seen Aug 2026
  • BST to Sorted Circular Doubly Linked ListONSITE INTERVIEW · Seen Aug 2026
  • Classify Natural-Month Distance Between DatesONSITE INTERVIEW · Seen Aug 2026
  • Copy a Random-Pointer List in Constant SpaceONSITE INTERVIEW · Seen Aug 2026

Similar questions at other companies

  • Single-Pass S-Expression ParserBaseten · ONSITE INTERVIEWBaseten interview questions
  • Flatten a Mixed JSON ListNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions
  • Molecular Weight of a Grouped FormulaAgoda · ONSITE INTERVIEWAgoda interview questions
  • Calculate a Token-List ExpressionCollege Vine · ONSITE INTERVIEW
  • Rich Text Parser Part 3 (Depth Tokens)Figma · PHONE SCREEN · ONSITE INTERVIEWFigma interview questions
  • Inorder Tree String EquivalenceGoogle · ONSITE INTERVIEWGoogle interview questions
  • Lisp Expression ParserAmazon · ONSITE INTERVIEW
  • Lisp Expression ParserAttentive · ONSITE INTERVIEWAttentive interview questions
  • Flatten Nested Map Paths with a StackSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Dictionary Records to an Editable XML TreeSnap Inc. · ONSITE INTERVIEWSnap Inc. interview questions

Apple interview experiences

  • Apple SWE Phone Screen: Subarray Products: 1 Experience
  • Apple New Grad SWE Java, Coding and Manager Rounds: 1 Experience
  • Apple AI Coding and Related Interviews: 17 Experiences
  • Apple India SWE New Grad Interviews: 5 Experiences
  • Apple New Grad SWE Phone Screens: 12 Experiences (Part 3 of 3)
  • Apple New Grad SWE and Embedded: 16 Experiences (Part 2 of 3)
All Apple interview questionsSee Apple hiring insights
Saved
class Solution {
    public String[] preorderElementNames(String html) {
        // Write your code here.
    }
}
Checking account…