PracHub
QuestionsCoachesLearningGuidesInterview Prep

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.

Given level-order tree values, return the diameter in edges using DFS that returns height and best diameter.

Constraints

  • Inputs are provided as Python literals matching the function signature.
  • Return a deterministic exact-match result.

Examples

Input: ([1,2,3,4,5],)

Expected Output: 3

Explanation: Diameter through root.

Input: ([],)

Expected Output: 0

Explanation: Empty tree.

Input: ([1],)

Expected Output: 0

Explanation: Single node.

Input: ([1,2,None,3,None,4],)

Expected Output: 2

Explanation: Skewed tree.

Hints

  1. Choose a representation that makes the core operation simple.
  2. Handle empty and boundary inputs before the main algorithm.
Last updated: Jun 27, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ 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
  • AI Coding 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.

Related Coding Questions

  • Find Shortest Unique Prefixes - Meta (medium)
  • Compute Exclusive Execution Times - Meta (medium)
  • Solve Tree Columns And Maze Variants - Meta (medium)
  • Solve Tree Diameter and Palindromic Counts - Meta (medium)
  • Simulate Monster Team Battles - Meta (hard)