← DoorDash Interview Insights

DoorDash·Software Engineer·Technical Phone Screen·Senior

SeniorPrefer not to say
Apr 2026

Summary

DoorDash SWE interview focused on a tree diameter variant that kept evolving into harder and harder follow-ups. The core problem was manageable but the online update extension pushed things into territory I wasn't fully prepared for.

Questions Asked (3)

Q1

Given a binary tree where the alive nodes are defined as the leaves, write an O(n) time and O(h) space algorithm to find the maximum number of edges between any two alive nodes.

Algorithms & Data Structures
Author's notes

This is basically tree diameter and I'd seen it before, so the DFS approach came pretty naturally.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a post-order DFS that returns the height of each subtree and tracks the maximum diameter (number of edges) among leaf nodes. At each node, combine the heights of its left and right subtrees to update the global maximum, but only consider paths that start and end at leaves.

Pro tip: Clarify that 'alive nodes' are leaves and that the path must be between two leaves, not any nodes. Mention that the algorithm should handle edge cases like a single leaf or a tree with only one leaf, where the answer is 0.

1. Clarify definitions and constraints

Confirm that alive nodes are leaves, and the path must be between two distinct leaves. Discuss edge cases: empty tree, single leaf, and tree with only one leaf.

2. Design recursive DFS function

Define a function that returns the height (max edges to a leaf) of the subtree rooted at the current node. For a leaf, return 0; for null, return -1 or a sentinel.

3. Compute diameter at each node

At each internal node, if both left and right subtrees contain at least one leaf, update the global maximum with left_height + right_height + 2. If only one side has leaves, propagate the height from that side.

4. Handle single-leaf subtrees

If a node has only one child that contains leaves, return that child's height + 1. This ensures the height correctly represents the distance to the nearest leaf in that subtree.

5. Analyze complexity and finalize

Explain that the DFS visits each node once, giving O(n) time, and the recursion stack uses O(h) space. Return the global maximum as the answer.

Key Points to Mention

  • Post-order traversal to compute subtree heights and update diameter.
  • Only consider paths where both endpoints are leaves; ignore internal nodes as endpoints.
  • Use a global variable to track the maximum diameter.
  • Handle cases where a subtree has no leaves (return a sentinel like -1).
  • Time complexity O(n) and space complexity O(h) due to recursion stack.
  • Edge cases: single leaf (answer 0), empty tree, and skewed trees.

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

Q2

Now each node has an arbitrary boolean alive flag instead of just leaves being alive. Compute the maximum distance between any two alive nodes, where intermediate nodes along the path can be anything.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This tripped me up more than I expected.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a post-order traversal to compute, for each node, the maximum distance from that node to any alive node in its subtree. At each node, combine the two largest such distances from different children to update the global maximum, and return the maximum distance (or 0 if the node itself is alive) to the parent.

Pro tip: Clarify edge cases upfront: if there are fewer than two alive nodes, the answer is 0; also confirm whether the distance is measured in edges or nodes, as this affects the base case and final result.

1. Clarify requirements and edge cases

Ask about the definition of distance (edges vs. nodes), whether the tree is binary or general, and what to return if there are 0 or 1 alive nodes.

2. Define recursive state

For each node, compute the maximum distance from that node to any alive node in its subtree. If no alive node exists, return a sentinel like -1.

3. Post-order traversal and combine

Recursively process children, collect their returned distances, and at the current node, consider the two largest distances from different children to form a path through the node.

4. Update global maximum

Maintain a global variable for the maximum distance found so far. At each node, update it with the sum of the two largest child distances (if both exist) and also consider the node itself if alive.

5. Return and finalize

Return the maximum distance from the current node to an alive node in its subtree (including itself if alive) to the parent. After traversal, the global maximum is the answer.

Key Points to Mention

  • Post-order traversal to process children before parent
  • Maintaining the top two longest paths from different children at each node
  • Handling nodes with no alive descendants using a sentinel value (e.g., -1)
  • Considering the node itself as a potential endpoint if its alive flag is true
  • Time complexity O(n) and space complexity O(h) for recursion stack
  • Edge cases: fewer than two alive nodes, all nodes alive, skewed tree

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

Q3

Support online updates where a node's alive flag can be toggled at any time, and after each update you need to return the current maximum distance between alive nodes.

Algorithms & Data StructuresSystem DesignTechnical Trade-offs
Author's notes

Yeah this is where things fell apart a bit.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem constraints (tree vs graph, number of nodes, update frequency) and discuss trade-offs between recomputation and dynamic data structures. Propose a solution using tree diameter properties (e.g., maintaining farthest nodes via LCA and segment trees) or a dynamic programming approach with lazy updates. Outline the algorithm, complexity, and potential optimizations for online updates.

Pro tip: Emphasize that in a tree, the diameter endpoints are always among the farthest nodes from any node, and you can maintain them using two BFS/DFS passes; for dynamic updates, consider using a segment tree over Euler tour to query farthest alive node quickly.

1. Clarify requirements and constraints

Ask about the graph type (tree vs general graph), number of nodes, frequency of updates, and whether updates are online. Confirm if the distance metric is edge count or weighted.

2. Discuss naive and optimized approaches

Mention that recomputing diameter after each update via BFS/DFS is O(N) per update, which may be too slow. Propose maintaining the diameter endpoints dynamically using data structures like segment trees or balanced BSTs.

3. Outline the chosen algorithm

For a tree, maintain the set of alive nodes and the current diameter endpoints. When a node toggles, update the set and recompute the diameter by checking distances from the new node to existing endpoints and between endpoints. Use LCA for O(log N) distance queries.

4. Analyze complexity and trade-offs

State that each update takes O(log N) time with O(N log N) preprocessing, or O(1) if using a different structure. Discuss memory vs time trade-offs and scalability.

5. Handle edge cases and extensions

Consider cases with 0 or 1 alive node, all nodes dead, or multiple components. If the graph is not a tree, mention that the problem becomes harder (e.g., dynamic graph diameter) and may require approximation or different techniques.

Key Points to Mention

  • Tree diameter property: endpoints are farthest nodes from any node
  • Use of LCA (Lowest Common Ancestor) for O(log N) distance queries
  • Segment tree or Fenwick tree over Euler tour to maintain alive nodes
  • Trade-offs between recomputation and dynamic maintenance
  • Handling toggles: adding/removing nodes from the alive set
  • Complexity analysis: O(log N) per update, O(N log N) preprocessing

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