Deserialize XML Into a Tree
Updated
Problem statement
Given a string xml that holds one XML element, build the tree it describes (deserialize it) and return that tree as a list of lines.
The document uses only these parts:
- A start tag such as
<name a="1" b="2">and its end tag</name>. Everything between them is the element's children: other elements and text, in order. - An empty-element tag such as
<name a="1"/>, which is an element with no children. - Text: the characters between two tags. Remove the spaces at both ends. If nothing is left, ignore the text.
- Entities: in text and in attribute values,
<,>,&,"and'stand for<,>,&,"and'. Each entity is replaced exactly once, so&lt;becomes<.
Write the tree in document order. For an element at depth d (the root has depth 0), let pad be 2 * d spaces. The element gives:
- one line:
padfollowed by the element's name; - then one line per attribute, in the order written:
pad, two spaces,@, the name,=and the value; - then its children in order: a child element by these same rules at depth
d + 1, and a text child aspad, two spaces,#and the text.
Function
parseXml(xml: String) → List<String>Examples
Example 1
xml = "<shelf room=\"A1\"><book id=\"7\" lang=\"en\">Dune</book><book id=\"9\"/></shelf>"return = ["shelf"," @room=A1"," book"," @id=7"," @lang=en"," #Dune"," book"," @id=9"]The root shelf has one attribute and two book children. The first book has two attributes (listed in the order written) and the text Dune. The second book is an empty-element tag, so it has no children.
Example 2
xml = "<note> Fish & chips <b>hot</b> 5 < 6 </note>"return = ["note"," #Fish & chips"," b"," #hot"," #5 < 6"]note has three children in this order: a text, the element b, and another text. Each text loses its outer spaces, and the entities become & and <.
Example 3
xml = "<a> <b><c/></b> <d x=\"\"/> </a>"return = ["a"," b"," c"," d"," @x="]The runs of spaces between tags are text with nothing left after trimming, so they are ignored. c is inside b, so it is two levels deep. d has an attribute with an empty value.
Constraints
1 <= xml.length <= 3000xmlhas only printable ASCII characters (codes 32 to 126), so no tabs or line breaks.xmlis one well-formed element (the root), with only spaces, if anything, before and after it. Elements are nested at most 100 deep.- Element and attribute names have 1 to 10 letters or digits and start with a letter. An element never repeats an attribute name.
- Each attribute is written
name="value", with one or more spaces before it and no spaces around=. Spaces may come before>or/>. End tags are written</name>with no spaces. - In text and attribute values,
<never appears and&appears only at the start of one of the five entities. Attribute values never contain"but may contain>and/.
More Adobe Phone Screen questions
- Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
- All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
- Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
- Binary Tree Level-Order Traversal by LevelsPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
- Count Occurrences in a Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
- Count Pairs with Absolute Difference KPHONE SCREEN · Seen Jul 2026
- Count Subsets with Sum KPHONE SCREEN · Seen Jul 2026
- Numbers with Exactly Three DivisorsPHONE SCREEN · Seen Jul 2026
- Sum of Even Nodes on a Path in a Numbered Binary TreePHONE SCREEN · Seen Jul 2026
- Largest Value in Every Sliding WindowPHONE SCREEN · Seen Jul 2026
Similar questions at other companies
- Remove Parentheses from a Plus-Minus ExpressionOracle · PHONE SCREENOracle interview questions
- Most Frequent Call Path From Function Trace LogsRoblox · PHONE SCREENRoblox interview questions
- Decode StringCompass International Holdings · PHONE SCREENCompass International Holdings interview questions
- Rich Text Parser Part 3 (Depth Tokens)Figma · PHONE SCREEN · ONSITE INTERVIEWFigma interview questions
- Calculator with ParenthesesGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Decode an Encoded StringAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Parse Indented YAML MappingsUber · PHONE SCREENUber interview questions
- Decode Repeated GroupsWaymo · PHONE SCREENWaymo interview questions
- Convert Email Text to Structured MarkupZip · PHONE SCREEN · ONSITE INTERVIEWZip interview questions
- Parse Variable, Expression, and Application TreesSambaNova Systems · PHONE SCREEN · ONSITE INTERVIEWSambaNova Systems interview questions