← LinkedIn Interview Insights

LinkedIn·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePending
Sep 2026Remote

Summary

LinkedIn SWE coding round where I got a tree problem, implemented a working iterative postorder traversal, and then spent the bulk of the interview in a back-and-forth about space complexity and a line-by-line dry run. Only finished one question total, which has me second-guessing how the round will be read.

Questions Asked (1)

Q1

Given a tree, return the nodes in the order they would be removed if you repeatedly removed all leaf nodes, grouping nodes by their height from the leaves.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

I went with an iterative postorder traversal using a stack where each frame tracked the node, a child iterator, and the max height seen so far among processed children.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem: this is essentially finding the height of each node from the leaves (leaf height = 0) and grouping nodes by height. Use a post-order traversal or topological peeling (Kahn's algorithm) to compute heights efficiently. Then present the solution with complexity analysis and discuss trade-offs between recursive and iterative approaches.

Pro tip: Mention that this problem is equivalent to finding the 'height' of each node in the tree when rooted at the leaves, and that it can be solved in O(n) time. Also, proactively discuss how to handle large trees to avoid stack overflow by using an iterative approach.

1. Clarify the problem and edge cases

Confirm that the tree is rooted (or unrooted) and that leaves are nodes with no children. Discuss edge cases like empty tree, single node, and skewed trees.

2. Define the height metric

Explain that the height of a node from leaves is the number of edges on the longest downward path to a leaf, with leaves having height 0. This is the key to grouping.

3. Choose an algorithm

Propose a post-order DFS to compute heights recursively, or an iterative topological peeling (Kahn's algorithm) that removes leaves layer by layer. Discuss trade-offs: recursion is simpler but may cause stack overflow; iterative is more robust for large trees.

4. Implement and group

During traversal, compute each node's height and add it to a list for that height. Alternatively, during peeling, assign the current layer number as the height.

5. Analyze complexity and test

State that time and space complexity are O(n). Walk through a small example to verify correctness, and mention potential optimizations like using an array of lists for grouping.

Key Points to Mention

  • The problem is equivalent to computing the height of each node from the leaves (leaf height = 0).
  • Post-order traversal (DFS) naturally computes heights bottom-up.
  • Iterative peeling (Kahn's algorithm) processes nodes layer by layer, which directly gives the grouping.
  • Time complexity is O(n) and space complexity is O(n) for storing the result and auxiliary data structures.
  • Recursive DFS may cause stack overflow for deep trees; iterative approach is safer.
  • The output should be a list of lists, where each inner list contains nodes at the same height, ordered from leaves upward.

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