FastPrepSerialize and Deserialize a Binary Tree
Problems/Serialize and Deserialize a Binary Tree

Serialize and Deserialize a Binary Tree

NVIDIA logoNVIDIA● HardFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The NVIDIA Deep Learning Software Engineer–Inference phone-screen report names binary-tree serialization and deserialization and emphasizes clean code and optimal complexity. It omits the wire format and size limits; this exercise supplies a conventional preorder/null-marker format and independently checks both directions. Approximate source match: 90%.

You will write both halves of a binary-tree codec. The encoder turns a tree into a string, and the decoder turns a string back into a tree. Your function gets a tree root and a string data. Encode root and decode data separately. The two do not have to describe the same tree.

Use preorder encoding. For each node, write its value as a signed decimal number, then encode its left subtree, then its right subtree. Write # wherever a pointer is null. Put exactly one comma between tokens, with no spaces and no comma at the end.

Return two strings, in this order:

  1. The preorder encoding of root, written exactly as described above.
  2. The tree decoded from data, written as compact level-order JSON. Level order lists the nodes level by level, left to right, and compact means no spaces. Keep null for missing nodes in the middle, but drop any null values at the end. An empty decoded tree is [].

You can assume data is always a valid preorder encoding that follows every format rule here, including the rules for writing numbers.

Function

binaryTreeCodec(root: TreeNode, data: String) → String[]

Examples

Example 1

root = [1,2,3,null,null,4,5]data = "8,#,9,#,#"return = ["1,2,#,#,3,4,#,#,5,#,#","[8,null,9]"]
root
12345

Encoding root writes each node, then its whole left subtree, then its right subtree. Every leaf is followed by two # marks for its missing children. Decoding data gives a top node with no left child and a single leaf as its right child. So its level-order list keeps a null before that leaf. The two trees are unrelated.

Example 2

root = []data = "#"return = ["#","[]"]
root
empty

root is empty, so its encoding is just #. Decoding data, which is only #, gives an empty tree, written as [].

Example 3

root = [-7,null,3,-1]data = "0,-2,#,#,0,#,#"return = ["-7,#,3,-1,#,#,#","[0,-2,0]"]
root
-73-1

In root, the top node has no left child, so a # comes right after its value. Its right child comes next, then that child's left child, then the remaining # marks. In data, the top node has two leaf children, and the right one repeats the top value. Negative values keep their minus sign, and each repeated value stays in its own place.

Constraints

  • Each input tree has from 0 through 500 nodes.
  • Every node value satisfies -10^6 <= value <= 10^6.
  • Numbers are written in decimal with no leading zeros and no plus sign. Zero is written as 0.
  • Both trees are finite binary trees with no cycles. Decoding must keep every value and every left and right link.

More NVIDIA Phone Screen questions

  • Network Layer Execution OrderPHONE SCREEN · Seen Nov 2026
  • Paged KV Cache AccessPHONE SCREEN · Seen Nov 2026
  • Sliding Window Attention over a KV CachePHONE SCREEN · Seen Nov 2026
  • Summarize Server Healthcheck LogsPHONE SCREEN · Seen Oct 2026
  • Hash Partition OffsetsPHONE SCREEN · Seen Oct 2026
  • Filter Paginated Medical RecordsPHONE SCREEN · Seen Oct 2026
  • Least Frequently Used CachePHONE SCREEN · Seen Oct 2026
  • Parallel String Counts with a Custom Hash TablePHONE SCREEN · Seen Oct 2026
  • Simulate a Reference-Counted Smart PointerPHONE SCREEN · Seen Aug 2026
  • Streaming Top-K Frequent ElementsPHONE SCREEN · Seen Aug 2026

Similar questions at other companies

  • Recover a Tree From Preorder TraversalGrammarly · PHONE SCREEN
  • Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
  • Find File Paths in a Directory TreeUber · PHONE SCREENUber interview questions
  • Serialize and Deserialize a Binary TreeAmazon · ONSITE INTERVIEW
  • Serialize and Deserialize a Binary TreeGoogle · ONSITE INTERVIEWGoogle interview questions
  • Count Distinct Rooted-Tree GraftsThe D.E. Shaw Group · PHONE SCREENThe D.E. Shaw Group interview questions
  • Vertical Order Traversal of a Binary TreeBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
  • Recover a Tree from Preorder Depth EncodingSuperhuman · PHONE SCREENSuperhuman interview questions
  • Account Roles With Parent-User InheritanceStripe · PHONE SCREENStripe interview questions
  • Validate a BST with a Custom ComparisonApple · PHONE SCREENApple interview questions

NVIDIA interview experiences

  • NVIDIA Full-Time SWE Onsite Scheduling: 1 Experience
  • NVIDIA OA: What It Tests (2026)
  • NVIDIA DevOps, QA and Infra Intern Interviews: 12 Experiences
  • NVIDIA Ignite Internship OA Experience
  • NVIDIA Software Intern Interview Questions: 9 Experiences
  • NVIDIA Ignite SWE Intern OA Questions: 6 Experiences
All NVIDIA interview questionsSee NVIDIA hiring insights
Saved
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public String[] binaryTreeCodec(TreeNode root, String data) {
        // Write your code here
    }
}
Checking account…