FastPrepDeserialize XML Into a Tree
Problems/Deserialize XML Into a Tree

Deserialize XML Into a Tree

Adobe logoAdobe● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The task: turn a small XML document into a tree of elements, attributes and text, then print that tree in a fixed line format.

An Adobe CS2 report from Bangalore (June 2024 interviews, posted September 2024, ending in an offer) lists two items in its first round. The first is a coding problem that uses a heap. The second is this one. The report only calls it building an XML deserializer. It gives no XML features, no input or output shape and no examples.

We chose these rules: a small XML subset with nested elements, double-quoted attributes, empty-element tags, text and the five standard entities. There are no comments, declarations or CDATA. Input is always well-formed. Text is trimmed at both ends and dropped when blank. The result uses the indented line format. We wrote new examples.

Our version is about 60% the same as the reported question.

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, &lt;, &gt;, &amp;, &quot; and &apos; stand for <, >, &, " and '. Each entity is replaced exactly once, so &amp;lt; becomes &lt;.

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: pad followed 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 as pad, 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 &amp; chips <b>hot</b> 5 &lt; 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 <= 3000
  • xml has only printable ASCII characters (codes 32 to 126), so no tabs or line breaks.
  • xml is 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

Interview reports that mention this question

  • Adobe CS2 and Senior CS Interviews: 6 Experiences (Part 5 of 6)
  • Adobe Experienced Hire Interviews: 55 Experiences (Part 1 of 6)

Adobe interview experiences

  • Adobe Web, QA, DevOps, ML Roles: 13 Experiences (Part 3 of 3)
  • Adobe SWE Interviews, Level Unknown: 25 Experiences (Part 2 of 3)
  • Adobe Interviews, Level Unknown: 38 Experiences (Part 1 of 3)
  • Adobe New Grad MTS Interview Questions: 7 Experiences
All Adobe interview questionsSee Adobe hiring insights
Saved
class Solution {
    public List<String> parseXml(String xml) {
        // Write your code here.
    }
}
Checking account…