PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Meta

Compute binary-tree diameter via return-only DFS

Last updated: Mar 29, 2026

Quick Overview

This question evaluates algorithmic problem-solving with binary trees, focusing on tree traversal techniques, recursion versus iterative implementations, and analysis of time and space complexity including worst-case input shapes such as skewed trees.

  • Medium
  • Meta
  • Coding & Algorithms
  • Data Scientist

Compute binary-tree diameter via return-only DFS

Company: Meta

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

Given the root of a binary tree, compute its diameter defined as the number of edges on the longest path between any two nodes. Implement a DFS that returns a tuple to its caller: (height_of_subtree, best_diameter_in_subtree). Combine children’s return values at each node to update the best diameter without using any external list, array, or global variable. Requirements: O(n) time, O(h) auxiliary space where h is the tree height. Edge cases: empty tree (diameter = 0), single node (diameter = 0), completely skewed tree. Then answer: (1) Why does accumulating traversal state in a list during DFS inflate space from O(h) to O(n), and under what input shapes does it hurt most? (2) Provide an iterative postorder version using an explicit stack that still achieves O(h) auxiliary space. (3) State and justify the time and space complexities for both versions.

Quick Answer: This question evaluates algorithmic problem-solving with binary trees, focusing on tree traversal techniques, recursion versus iterative implementations, and analysis of time and space complexity including worst-case input shapes such as skewed trees.

Related Interview Questions

  • Solve Tree Columns And Maze Variants - Meta (medium)
  • Solve Tree Diameter and Palindromic Counts - Meta (medium)
  • Simulate Monster Team Battles - Meta (hard)
  • Solve a Key-Door Corridor Maze - Meta (medium)
  • Solve Array Merge and Parentheses Cleanup - Meta (medium)
Meta logo
Meta
Oct 13, 2025, 9:49 PM
Data Scientist
Technical Screen
Coding & Algorithms
4
0

Given the root of a binary tree, compute its diameter defined as the number of edges on the longest path between any two nodes. Implement a DFS that returns a tuple to its caller: (height_of_subtree, best_diameter_in_subtree). Combine children’s return values at each node to update the best diameter without using any external list, array, or global variable. Requirements: O(n) time, O(h) auxiliary space where h is the tree height. Edge cases: empty tree (diameter = 0), single node (diameter = 0), completely skewed tree. Then answer: (1) Why does accumulating traversal state in a list during DFS inflate space from O(h) to O(n), and under what input shapes does it hurt most? (2) Provide an iterative postorder version using an explicit stack that still achieves O(h) auxiliary space. (3) State and justify the time and space complexities for both versions.

Submit Your Answer

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Meta•More Data Scientist•Meta Data Scientist•Meta Coding & Algorithms•Data Scientist Coding & Algorithms
PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.