← Grammarly Interview Insights

Grammarly·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jun 2026Remote

Summary

Grammarly SWE interview that had a twist on a classic tree problem. Instead of just solving it, you had to build the tree first from serialized input, which added a layer I wasn't fully expecting.

Questions Asked (1)

Q1

You're given a serialized sequence of numbers representing a binary tree (level-order with nulls, or similar). Build the tree from that input, then compute its maximum depth.

Algorithms & Data StructuresTechnical Trade-offsSystem Design
Author's notes

I knew the depth part cold, that's just a standard recursion problem.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify the serialization format (e.g., level-order with nulls) and edge cases. Then, implement tree construction using a queue, and finally compute maximum depth via recursion or iterative BFS. Discuss trade-offs between approaches and analyze time/space complexity.

Pro tip: Mention that the depth can be computed during construction to save a pass, but clarify that separate steps improve readability and modularity. Also, note that recursion depth could be an issue for very skewed trees, so an iterative BFS might be safer in production.

1. Clarify input format and edge cases

Ask whether the serialization is level-order with null markers, and confirm handling of empty input or single-node trees. This ensures alignment with the interviewer and avoids incorrect assumptions.

2. Design tree construction algorithm

Use a queue to process nodes level by level: create the root from the first value, then for each node, assign left and right children from subsequent values, skipping nulls. Explain why a queue is appropriate for level-order reconstruction.

3. Implement maximum depth computation

Choose between recursive DFS (post-order) or iterative BFS. For recursion, depth = 1 + max(depth(left), depth(right)); for BFS, count levels until queue is empty. Discuss trade-offs: recursion is concise but risks stack overflow; BFS uses extra space but is safe for deep trees.

4. Analyze complexity and optimize

State that both construction and depth computation are O(n) time. Space is O(n) for the queue during construction and O(h) for recursion or O(w) for BFS, where h is height and w is max width. Mention that depth could be computed during construction to save a pass, but separate steps are clearer.

5. Test with examples and edge cases

Walk through a small example (e.g., [1,2,3,null,4]) to verify correctness. Test edge cases: empty tree, single node, skewed tree, and complete tree. Discuss how the algorithm handles nulls and missing children.

Key Points to Mention

  • Level-order serialization format and null handling
  • Queue-based reconstruction for level-order input
  • Recursive vs iterative depth computation trade-offs
  • Time and space complexity analysis (O(n) time, O(n) space)
  • Edge cases: empty tree, single node, skewed tree
  • Potential optimization: compute depth during construction

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