Maximum Depth of Binary Tree

Asked atBlinkitInfosysMetaSpotify
1Give yourself 5 minutes
2Answer out loud, not in your head
3Then compare with the answer below
0
Stuck? Show a way to structure it+
  1. 01Define the depth of an empty subtree as zero.
  2. 02Recursively compute left and right subtree depths.
  3. 03Return one plus the larger child depth.
  4. 04State whether the definition counts nodes or edges and handle an empty tree.

Reference answer

Then expect these follow-ups

  • How would you compute minimum depth?

    Tests: tree edge cases

  • When would BFS use less memory than DFS here?

    Tests: trade-offs

Hiring now

Spotify has 77 open roles right now

landed applies to roles like these for you, on each company’s own site, with answers written for the role.

Get 5x interviews10 free auto-applies. No card.
  • Senior Software Engineer

    Remote

  • Senior Director, Global Advertising Product Marketing

    New York, NY

  • Head of Measurement & Advertising Effectiveness

    New York, NY

Or practice this question first

Next question