November 29, 2020

“Poisonous people do not deserve your time. To think otherwise is masochistic.”

- Tim Ferriss

Day 9: Lowest Common Ancestor

You're given a binary search tree and two of its child nodes as parameters. Can you write a method lowestCommonAncestor(root: Node, node1: Node, node2: Node) to identify the lowest common ancestor of the two nodes? The lowest common ancestor is the deepest node that has both of the two nodes as descendants.

In the below example, the lowest common ancestor of node 5 and 8 is 7.

      / \
     4   8
    / \
   1   5

The method will called in the following manner: lowestCommonAncestor(root, node1, node2);.

You can assume our standard node definition for the tree vertices:

function Node(val) {
  this.val = val;
  this.left = this.right = null;

Get the Solution →

Have you seen this question before? We are frequently adding problems and may have updated the order. You can change the day that you're on in the settings panel. As always, you can see the solution and a full step by step explanation at this link.

Our technical interview course gets results. Love the daily emails? AlgoDaily exists to provide high quality coding interview prep and career resources to developers from non-traditional paths. In the course, you'll find:

  • One year, unlimited access to the full guided interview course with over 150 hours of interview prep material (a $79 value)
  • PDF, Mobi, and ePub copies of the 732 page ebook The AlgoDaily Book: Core Essentials! (a $29 value)
  • Access to over 500 illustrations and visualizations found nowhere else
  • Over 100+ in-depth and well explained interview solutions
  • Access to a growing premium video solutions collection. Currently at nearly. 5 hours (35 videos) of content, with 2-4 videos being added every week (a $79 value)
  • Access to an exclusive jobs application tracking tool (usually $10/month)
  • Restart the AlgoDaily newsletter challenges from any day
  • All problem solutions submitted by the community
  • New material on interview techniques, career success, and breaking into software added weekly
  • BLACK FRIDAY SALE: All of the above, only $79+ $39 for one year of access

Become An Affiliate

If you're a fan of and would like to share the love, we want to reward you! If you run an app, community, blog, or simply have lots of friends interested in the course-- get yourself a nice side revenue stream. Sign up to be an affiliate of Affiliates get a whopping 50% commission on subscriptions referred.

How does it work? Sign up here, and start sharing your link. When people make a purchase on, you'll accrue 50% of the purchase price. Affiliates get paid out on month end.

One last thing-- if you liked this email, and found value in it, could you kindly forward it to a friend who might benefit?

Our mission is to make tough technical interviews accessible to all, especially those from non-traditional CS backgrounds. I'd deeply appreciate your help in spreading the word!


Get Free Daily Interview Problems

We'll send you the 100 most common coding interview questions, once a day with visual explanations. Join over 12,848 users who are doubling their salaries in 30 minutes a day. All subscribers get a free 86-page preview PDF with a week of study material.

Terms and Conditions

Ready for your dream job?

Welcome to the most accessible guide to technical interviews. Here's a preview of how the platform will look.

We believe that technical interviews are a matter of practicing well. We've referenced hundreds of resources on habit change, education design, and algorithms to design the best and most streamlined learning experience.

Learn more