Return Maximum number of turns in a tree from root to lead node

Asked atNutanix
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. 01Clarify whether leaf node and left-right turns are intended
  2. 02Traverse from root carrying previous direction and count
  3. 03Increment only when current edge changes direction
  4. 04Update a global or returned maximum at leaves
  5. 05Test single paths and alternating directions

Reference answer

Then expect these follow-ups

  • How would you return the leaf path that achieves the maximum?

    Tests: path reconstruction

  • How would you write this iteratively with an explicit stack?

    Tests: tree traversal

Hiring now

Nutanix has 108 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.
  • DevOps Engineer - AI Gateway

    Vancouver, British Columbia

  • Senior Engineering Manager

    Vancouver, British Columbia

  • Advisory Enterprise Account Manager

    Remote

Or practice this question first

Next question