FastPrepDictionary Records to an Editable XML Tree
Problems/Dictionary Records to an Editable XML Tree

Dictionary Records to an Editable XML Tree

Snap Inc. logoSnap Inc.● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Snap Inc. onsite interview. The report asks you to rebuild XML from dictionary records and to support adding and removing elements. It does not give the record layout, how edits are done, the order of children, how far a removal reaches, or the exact output text. Our version is about 90% the same as the reported question.

Build one XML tree from dictionary-style records, then apply an ordered sequence of edits. Each row of records is [id, parentId, tag, text]. Exactly one row has an empty parentId and represents the root. Every other row names its parent by ID.

Each row of operations is [kind, id, parentId, tag, text]:

  • For add, create a new leaf with the supplied ID, parent ID, tag, and text. Append it after the parent’s existing children.
  • For remove, remove the non-root element with the supplied ID together with its entire subtree. The remaining three fields are empty strings.

After every operation, serialize the current root and append that XML string to the result. Preserve child order. Always use an explicit opening and closing tag, including for an empty element. In text, escape &, <, and > as &amp;, &lt;, and &gt;, respectively.

Function

editXml(records: String[][], operations: String[][]) → String[]

Examples

Example 1

records = [["root","","catalog",""],["a","root","item","A&B"]]operations = [["add","b","root","item","<B>"],["remove","a","","",""]]return = ["<catalog><item>A&amp;B</item><item>&lt;B&gt;</item></catalog>","<catalog><item>&lt;B&gt;</item></catalog>"]

The first edit appends element b after a and escapes both text values. The second edit removes element a, leaving only b.

Example 2

records = [["r","","library",""],["book","r","book",""],["title","book","title","Algorithms"]]operations = [["add","note","book","note","fast & clear"],["remove","book","","",""]]return = ["<library><book><title>Algorithms</title><note>fast &amp; clear</note></book></library>","<library></library>"]

The new note is appended to the existing book subtree. Removing book also removes its title and note descendants.

Constraints

  • 1 <= records.length <= 500.
  • 0 <= operations.length <= 500.
  • Each record has exactly four fields and each operation has exactly five fields.
  • IDs are unique nonempty strings of at most 30 printable ASCII characters.
  • Tags match [A-Za-z][A-Za-z0-9_-]* and have length at most 30.
  • Text has at most 100 printable ASCII characters.
  • The initial records form one valid rooted tree, although a child may appear before its parent.
  • Every operation is valid when it is applied: an added ID is new, its parent exists, and a removed ID exists and is not the root.
  • The total serialized output contains at most 10^6 characters.

More Snap Inc. interview questions

  • Render an Org Chart and Find Skip-Level PairsONSITE INTERVIEW · Seen Nov 2026
  • Peak of an Increasing-Then-Decreasing ArrayONSITE INTERVIEW · Seen Oct 2026
  • Number of Closed IslandsONSITE INTERVIEW · Seen Oct 2026
  • Radar Barrier CrossingONSITE INTERVIEW · Seen Oct 2026
  • Sort Characters by FrequencyONSITE INTERVIEW · Seen Sep 2026
  • Metric Counter with a Fake ClockONSITE INTERVIEW · Seen Aug 2026
  • ID Allocator with Assign and ReleasePHONE SCREEN · Seen Nov 2026
  • Basic Calculator IIPHONE SCREEN · Seen Oct 2026
  • Stream Cluster Max and MedianPHONE SCREEN · Seen Oct 2026
  • LRU Cache with Thread-Safety Follow-UpPHONE SCREEN · Seen Sep 2026

Similar questions at other companies

  • Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
  • File System Shell CommandsHarvey · ONSITE INTERVIEWHarvey interview questions
  • Invoice / Payment ReconciliationStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
  • In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
  • Insert an Employee into an Organization HierarchyWaymo · PHONE SCREENWaymo interview questions
  • Machine Message RouterOpenAI · PHONE SCREENOpenAI interview questions
  • JSON Identity VerificationKikoff · PHONE SCREENKikoff interview questions
  • Hidden ArtifactsIMC Trading · OAIMC Trading interview questions
  • Route Pattern MatcherGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
  • Most Frequent Call Path From Function Trace LogsRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions

Interview reports that mention this question

  • Snap AI Coding Interview, Experienced SWE and MLE: 4 Experiences

Snap Inc. interview experiences

  • Snap Experienced MLE Onsite Interview Questions: 1 Experience
  • Snap Experienced MLE Virtual Onsite: 1 Experience
All Snap Inc. interview questionsSee Snap Inc. hiring insights
Saved
class Solution {
    public String[] editXml(String[][] records, String[][] operations) {
        // Write your code here.
    }
}
Checking account…