FastPrepRNG-Based Tree Document Codec

RNG-Based Tree Document Codec

Baseten logoBaseten● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Implement a paired tree-document codec: serialize and encrypt an ordered rooted tree, or decrypt and deserialize its ciphertext. For this exercise, use the deterministic practice conventions below.

The aligned arrays operations, seeds and inputs describe independent calls. Reset the random generator to seeds[i] at the start of each call.

  • For encrypt, inputs[i] contains alternating parent-index and label strings. Node 0 has parent -1. Each later node has an earlier node as its parent; its index is its position in this node order. Return a one-element array containing the ciphertext.
  • For decrypt, inputs[i] contains one ciphertext string. Return the recovered alternating parent-index and label strings in the original node order.

Serialize a tree with n nodes as the ASCII string n;parent:label;parent:label;.... Use canonical decimal counts and parent indices, with no padding or spaces; include the semicolon after every node.

For each serialized byte, update an unsigned 32-bit state in order: state ^= state << 13, state ^= state >> 17, state ^= state << 5. Left-shift phases keep only the low 32 bits, and right shift is logical. XOR the byte with the low eight bits of that updated state, then encode the result as exactly two lowercase hexadecimal digits. Decryption uses the same updated byte sequence to reverse the XOR and reconstruct the tree.

All decrypt inputs are valid ciphertexts produced by this exact codec with their supplied seeds. Complete treeDocumentCodec and return one output array per operation. The representation, serialization and xorshift32 recurrence complete the reported RNG-based tree encrypt/decrypt task as practice facts.

Function

treeDocumentCodec(operations: String[], seeds: long[], inputs: String[][]) → String[][]

Examples

Example 1

operations = ["encrypt"]seeds = [1]inputs = [["-1","a","0","b"]]return = [["133ae87eebb121821f16f0"]]

The ordered two-node tree serializes to 2;-1:a;0:b;. Each ASCII byte uses the next low-byte key.

Example 2

operations = ["decrypt","encrypt"]seeds = [1,42]inputs = [["133ae87eebb121821f16f0"],["-1","root"]]return = [["-1","a","0","b"],["19972ef1fec4b7c1e70c"]]

The first call recovers the exact parent/label representation. The second begins a fresh random stream for a separate one-node tree.

Constraints

  • The three arrays have equal length m, where 0 <= m <= 40.
  • Every operation is encrypt or decrypt; 1 <= seeds[i] <= 2^32 - 1.
  • Each represented tree has 1 <= n <= 200 nodes. Across all operations, represented node counts total at most 2000.
  • For encrypt inputs, there are exactly 2*n strings. The first parent is -1; node j > 0 has parent 0 <= parent < j.
  • Labels contain 1 through 16 lowercase ASCII letters. Labels may repeat.
  • For decrypt inputs, there is exactly one even-length lowercase hexadecimal ciphertext string representing a valid tree under the given seed.
  • Each operation resets its state; one complete state transition supplies one serialized-byte key.
  • The sum of serialized ASCII byte counts across all operations is at most 20000. For decrypt inputs, a call contributes half its ciphertext length. This practice bound keeps every result within the grading output budget.

More Baseten problems

See Baseten hiring insights
public String[][] treeDocumentCodec(String[] operations, long[] seeds, String[][] inputs) {
    // Write your code here.
}
operations["encrypt"]
seeds[1]
inputs[["-1","a","0","b"]]
expected[["133ae87eebb121821f16f0"]]
Checking account…