RNG-Based Tree Document Codec
Updated
Problem statement
Write both directions of a codec for tree documents. One direction turns an ordered rooted tree into text and encrypts it. The other decrypts the text and rebuilds the tree. Follow the exact rules below.
The entries at the same index in operations, seeds and inputs together describe one call. Each call is independent. At the start of each call, reset the random generator to seeds[i].
- For
encrypt,inputs[i]lists the nodes as alternating strings: a parent index, then a label, for each node in order. A node's index is its position in this order. Node0has parent-1. Every other node's parent comes earlier in the order. Return an array with one element: the ciphertext. - For
decrypt,inputs[i]holds one ciphertext string. Return the recovered alternating parent-index and label strings, in the original node order.
To serialize a tree with n nodes, write the ASCII string n;parent:label;parent:label;.... Write the node count and the parent indices as plain decimal numbers, with no padding or spaces. Put a semicolon after every node.
The random generator keeps an unsigned 32-bit state. For each byte of the serialized string, first update the state with these three steps, in order: state ^= state << 13, state ^= state >> 17, state ^= state << 5. After each left shift, keep only the low 32 bits. The right shift is logical (it fills with zeros). Then XOR the byte with the low eight bits of the new state. Write the result as exactly two lowercase hexadecimal digits. To decrypt, build the same key bytes from the same seed, undo the XOR, and rebuild the tree.
Every decrypt input is a valid ciphertext made by this exact codec with its given seed. Complete treeDocumentCodec and return one output array per operation. The tree format, the serialization and the xorshift32 steps above fully define the encryption.
Function
treeDocumentCodec(operations: String[], seeds: long[], inputs: String[][]) → String[][]Examples
Example 1
operations = ["encrypt"]seeds = [1]inputs = [["-1","a","0","b"]]return = [["133ae87eebb121821f16f0"]]The two-node tree is root a with child b. It serializes to 2;-1:a;0:b;. Each byte is XORed with the low eight bits of the next generator state and written as two hex digits, which gives the ciphertext.
Example 2
operations = ["decrypt","encrypt"]seeds = [1,42]inputs = [["133ae87eebb121821f16f0"],["-1","root"]]return = [["-1","a","0","b"],["19972ef1fec4b7c1e70c"]]The first call decrypts the ciphertext from the first example with the same seed, so it returns that tree's strings again. The second call resets the generator to its own seed and encrypts a separate one-node tree whose only node is root.
Constraints
- The three arrays have the same length
m, where0 <= m <= 40. - Every operation is
encryptordecrypt. Each seed satisfies1 <= seeds[i] <= 2^32 - 1. - Each tree has
1 <= n <= 200nodes. The node counts of all operations add up to at most2000. - An encrypt input has exactly
2*nstrings. The first parent is-1. Nodej > 0has a parent with0 <= parent < j. - Each label has
1through16lowercase ASCII letters. Two nodes may have the same label. - A decrypt input has exactly one ciphertext string. It has even length, uses lowercase hexadecimal digits, and decodes to a valid tree with the given seed.
- Each operation starts from a fresh state. Each byte of the serialized string uses one full state update (all three steps) as its key.
- All serialized strings together have at most
20000ASCII bytes. A decrypt call counts as half the length of its ciphertext.
More Baseten interview questions
Similar questions at other companies
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Ordered IP FirewallDatabricks · PHONE SCREENDatabricks interview questions
- CIDR IPv4 Range IteratorOpenAI · PHONE SCREENOpenAI interview questions
- File System Shell CommandsHarvey · ONSITE INTERVIEWHarvey interview questions
- Dictionary Records to an Editable XML TreeSnap Inc. · ONSITE INTERVIEWSnap Inc. interview questions
- Decrement a Binary Number and Count Its OnesZipRecruiter · OAZipRecruiter interview questions
- Number of Steps to Reduce a Binary Number to OneDeloitte · OADeloitte interview questions
- Operations to Reduce a Binary String Value to ZeroMarshall Wace · OAMarshall Wace interview questions
- Route Pattern MatcherGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions