← Airbnb Interview Insights

Airbnb·Software Engineer·Technical Phone Screen·Senior

Senior
Jun 2026

Summary

Airbnb software engineer interview with a pretty involved tree problem that kept growing in scope. The follow-ups caught me off guard more than the main question did.

Questions Asked (3)

Q1

Given a binary tree where each node stores an integer and an array called target, determine whether target appears as a subsequence of the tree's inorder traversal. Elements must appear in the same relative order but don't need to be contiguous. Try to do it in O(n) time with O(1) auxiliary space by streaming the traversal instead of storing it.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The O(1) space constraint is what makes this interesting.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use Morris inorder traversal to achieve O(1) auxiliary space while streaming nodes in inorder sequence. Maintain a pointer into the target array and advance it whenever the current node's value matches the next target element; if the pointer reaches the end, return true. After traversal, return whether all target elements were matched.

Pro tip: Emphasize that Morris traversal temporarily modifies the tree but restores it, so the O(1) space claim holds; also mention that early termination is possible once the target is fully matched, which can save time.

1. Clarify problem and constraints

Confirm that the tree is a binary tree (not necessarily BST), target is an array of integers, and we need subsequence matching in inorder traversal. State the goal: O(n) time and O(1) auxiliary space.

2. Choose traversal method

Explain that standard inorder traversal uses O(h) stack space, so to achieve O(1) auxiliary space, use Morris inorder traversal which temporarily modifies the tree.

3. Stream and match

During Morris traversal, compare each visited node's value with the next expected element in target. If they match, advance the target index; if the index reaches the end, return true immediately.

4. Handle edge cases and termination

After traversal, return true only if all target elements were matched. Discuss edge cases: empty target (return true), empty tree (return false unless target empty), and duplicate values.

5. Analyze complexity and trade-offs

State that Morris traversal visits each node at most twice, giving O(n) time, and uses only a few pointers, achieving O(1) auxiliary space. Mention that it modifies the tree temporarily but restores it, and note the trade-off of increased code complexity versus space savings.

Key Points to Mention

  • Morris inorder traversal for O(1) space
  • Streaming comparison with a pointer into target
  • Early termination when target is fully matched
  • Time complexity O(n) and auxiliary space O(1)
  • Temporary tree modification and restoration in Morris traversal
  • Edge cases: empty target, empty tree, duplicate values

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q2

Modify the tree so that its inorder traversal contains target as a subsequence. You can either overwrite the value of an existing node or insert a new node anywhere in the tree, and each operation costs 1. Compute the minimum number of operations needed and output that count along with one valid set of edits that achieves it.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This is where I started sweating.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify the problem constraints and define what it means for the inorder traversal to contain the target as a subsequence. Then, model the problem as finding the minimum number of edits (overwrites or insertions) to transform the current inorder sequence into one that has the target as a subsequence, leveraging dynamic programming or greedy matching. Finally, reconstruct one optimal set of edits and map them back to tree operations.

Pro tip: Demonstrate awareness of trade-offs: mention that while a DP approach guarantees optimality, a greedy approach might suffice under certain constraints, and always validate with edge cases like empty tree or target longer than traversal.

1. Clarify problem and constraints

Ask about tree properties (e.g., BST, balanced), target length, and whether overwriting a node can change its value arbitrarily. Confirm that inorder traversal is fixed by tree structure and that insertions can be anywhere.

2. Extract inorder sequence

Perform an inorder traversal to get the current sequence of node values. This sequence is the basis for matching the target as a subsequence.

3. Compute minimum edits via DP or greedy

Use dynamic programming to find the minimum number of overwrites/insertions needed to make target a subsequence of the modified sequence. Alternatively, use a greedy two-pointer approach if overwrites are always cheaper than insertions.

4. Reconstruct edits

Backtrack through the DP table or record decisions to list the specific overwrites and insertions. Map insertions to tree positions (e.g., as left/right children) that preserve inorder order.

5. Validate and output

Verify that the modified tree's inorder traversal contains target as a subsequence and that the edit count is minimal. Output the count and the edits.

Key Points to Mention

  • Inorder traversal of a tree yields a sequence; subsequence matching requires preserving relative order.
  • Dynamic programming state: dp[i][j] = min edits to match first i target chars using first j inorder nodes.
  • Overwrite cost vs. insertion cost: both cost 1, so DP must consider both options.
  • Reconstruction of edits: track whether each target char was matched by overwrite, insertion, or skipped node.
  • Tree insertion positions: new nodes can be added as leaves without disrupting existing inorder order if placed correctly.
  • Edge cases: empty tree, target longer than traversal, target already a subsequence (0 edits).

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q3

Walk through the edge cases for both parts: what happens with an empty target array, duplicate values in either the tree or the target, or a target that is longer than the entire inorder traversal? Also state the time and space complexity for each part of the solution.

Algorithms & Data Structures
Author's notes

Empty target is trivially true as a subsequence, which I said immediately.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First clarify the problem: Part 1 likely checks if a target array is a valid subtree inorder sequence, and Part 2 likely checks if it's a contiguous subarray of the tree's inorder traversal. Then systematically walk through each edge case (empty target, duplicates, target longer than traversal), stating the expected behavior and how your code handles it. Finally, state time and space complexity for each part, justifying with the traversal and matching costs.

Pro tip: Emphasize that duplicates require careful handling—e.g., using indices or a two-pointer approach rather than value-based hashing—and mention that an empty target is trivially a subarray but may be invalid for subtree matching depending on problem constraints.

1. Clarify problem definitions

Restate what Part 1 and Part 2 are solving (e.g., subtree inorder match vs. contiguous subarray match) to ensure edge cases are interpreted correctly.

2. Handle empty target

State that an empty target is a valid subarray (vacuously) but for subtree matching it likely returns false unless the tree is also empty; explain your code's behavior.

3. Address duplicates

Explain that duplicates in tree or target require index-based or two-pointer matching to avoid false positives; mention that inorder traversal of a BST with duplicates may not be strictly sorted.

4. Handle target longer than traversal

State that if target length exceeds the inorder traversal length, it cannot be a subarray or subtree match, so return false immediately.

5. State complexities

For each part, give time and space complexity: e.g., O(n) time for traversal plus O(n*m) for naive matching, or O(n) with KMP; space O(n) for traversal storage.

Key Points to Mention

  • Empty target: subarray check returns true, subtree check returns false unless tree is empty
  • Duplicates: use index-based matching or two-pointer to avoid ambiguity
  • Target longer than traversal: immediate false
  • Time complexity: O(n) traversal + O(n*m) naive matching or O(n+m) with KMP
  • Space complexity: O(n) for storing inorder traversal, O(1) extra for matching if using two pointers
  • Clarify assumptions about tree type (BST vs. general binary tree) and whether duplicates are allowed

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.