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

Serialize and Deserialize a Binary Tree

Amazon logoAmazon● HardFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionExamples

Problem statement

Source note: Reported in an Amazon onsite loop for an experienced software engineer (full-time). In the hiring manager round, after behavioral questions, the coding question was the well-known task of turning a binary tree into a string and rebuilding the tree from it, with little time left in the round. The report names the question only and gives no format, examples or limits.

Here we fixed the string format (preorder, # for an empty child, commas between tokens) so both halves can be checked, and the function returns the string for one tree plus the level order of the tree rebuilt from a second string. We also chose the limits (up to 1000 nodes, values from -1000 to 1000, at most 500 levels) and the examples.

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

Design a way to serialize a binary tree into a string and to deserialize that string back into the same tree. Use this exact format:

  • Visit the nodes in preorder: a node, then its whole left subtree, then its whole right subtree.
  • Write each node as its integer value and each empty child as #.
  • Separate the tokens with single commas and no spaces.

For example, the empty tree is #, and a single node with value -6 is -6,#,#.

Both halves are checked in one call. You are given a tree root and a string data, which may describe a different tree. Return an array of two strings:

The problem statement continues
Pro

Examples

Example 1

root = [5,3,8,null,4]data = "5,3,#,4,#,#,8,#,#"return = ["5,3,#,4,#,#,8,#,#","[5,3,8,null,4]"]
root
5348

Preorder writes 5, then its left subtree: 3, an empty left child #, and 4 with two empty children (4,#,#). Then the right subtree 8,#,#. That gives 5,3,#,4,#,#,8,#,#.

Here data is the same string, so rebuilding it gives the same tree. Its level order is [5,3,8,null,4]: the null is the empty left child of 3, and the empty children of 4 and 8 at the end are dropped.

02 · Example 2
FastPrep Pro
Reported in 1 Amazon interview this week

Unlock this recently reported problem

FastPrep Pro gives you full access to interview problems reported within the last week.

  • Full problem statement and constraints
  • 2 more worked examples, explained
  • Guided hints and editorial
  • Run your code on real test cases
$8.25/month

$99 billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week

More Amazon Onsite Interview questions

  • LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Maximal SquareONSITE INTERVIEW · Seen Nov 2026
  • Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Diagonal Traversal of a Binary TreeONSITE INTERVIEW · Seen Nov 2026
  • Minimum Time to Spread Through a GridPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Optimal Account BalancingONSITE INTERVIEW · Seen Nov 2026
  • Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Cheapest Flights Within K StopsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • N-Ary Tree BFS CodecGoogle · ONSITE INTERVIEWGoogle interview questions
  • N-Ary Tree Codec OperationsLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
  • Serialize and Deserialize a Binary TreeMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Serialize and Deserialize an N-Ary TreeUber · ONSITE INTERVIEWUber interview questions
  • Serialize and Deserialize a Binary TreeNVIDIA · PHONE SCREENNVIDIA interview questions
  • Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
  • Spreadsheet Formula DependenciesAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
  • Vertical Order Traversal of a Binary TreeBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
  • Vertical Order Traversal of a Binary TreeTokopedia · ONSITE INTERVIEWTokopedia interview questions
  • In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions

Interview reports that mention this question

  • Amazon Experienced SDE Onsite Questions: 1 Experience

Amazon interview experiences

  • Amazon SDE Onsite Interview Questions: 9 Experiences
  • Amazon SDE II and L5 FDE Interview Questions: 4 Experiences
  • Amazon Full-Time OA Problems, Earlier Reports (Part 5 of 5)
  • Amazon Full-Time OA Problems, Recent Reports (Part 4 of 5)
  • Amazon New Grad OA Problems: Every Task We Have (Part 3 of 5)
See Amazon hiring insights
CodePython 3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import heapq
from collections import defaultdict
 
class Solution:
def solve(self, values, queries):
# Build the adjacency structure
n = len(values)
graph = defaultdict(list)
for u, v in queries:
graph[u].append(v)
 
best = [float("inf")] * n
heap = [(0, 0)]
while heap:
cost, node = heapq.heappop(heap)
if cost > best[node]:
continue
Run and Submit unlock with Pro
FastPrep Pro
Reported in 1 Amazon interview this week

Unlock this recently reported problem

FastPrep Pro gives you full access to interview problems reported within the last week.

  • Full problem statement and constraints
  • 2 more worked examples, explained
  • Guided hints and editorial
  • Run your code on real test cases
$8.25/month

$99 billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week