Microsoft Coding Interview Question and Answer: Lowest Common Ancestor
Skills:
Algorithm Basics80%
Key Takeaways
Solves the Lowest Common Ancestor problem in a Microsoft coding interview
Full Transcript
in this video I'm going to discuss a Microsoft interview question and answer and this problem is called the lowest common ancestor problem so let's just dive into it here's the problem we have a tree as you can see on the left and we're given two elements four and five and we're trying to find the common ancestor that's the lowest in level in this case the answer would be three because we have two common ancestors for these two elements one and three and obviously three is the lower one of them and the LCA the lowest common ancestor of three and five three and five would be three again so LCA could be one of the given elements and the LCA of four and two four and two would be one and then L CA of 6 and six the same element would be this element itself so how can we solve this problem if you want to try solving this problem yourself pause the video right here and see if you can come up with the solution and come back to the video later there's a number of solutions to this problem but the one I find the simplest is the following let's say we're trying to find the LCA the lowest common answer of four and five what I would do first is I would find the path from the RO to one of the elements four let's say I do that by going to the left and then left again and right there I find the element so the pass to that element would be 1 14 134 and I would do the same thing with five so I start at the root again 1 three to the left and five is not there so I go to the right left again and I find the PATH so that's 1 3 65 and by examining these two paths 134 and 1365 we see that these two paths are the same up until three and converge or diverge after that so in these two paths we can see these two elements are the same so we return the last element in this common pass as the LCA of these two elements now the key to implementing this solution is this function pass to X it takes the root and the element that we're looking for X or five in this case and it returns a stack of this path or it returns this this path as a stack so it would look like this 1 3 6 five I'm just going to write it as a list for now and then same thing for this one four so the path is 134 so I'm going to write 134 right here and given these two paths we're able to to look at each element at a time and see which ones are the same until we find different elements six and four and we stop right here and we can just return this element as the LCA now if you want to try implementing this solution yourself pause the video right here and come back to it later when you're done here's my implementation to our solution and in particular the PA to X function let's say as an example we are trying to find the path from Rue to five this element and we have a recursive solution here if the element or the root that we're looking at right now is null then we just return no and if we're looking at the element that we were just looking for if root do value is equal to this one then we create a stack with this element alone and then we return that and let's say we're looking at this element now and if the left pass or if the left child contains a pass to X then that means left path is going to be not null and so we return left pass after adding the current element that we're looking at this one to the left pass and then we return that and we do the same thing with the right pass if the right pass let's say we're looking at this element If the righted child contains a pass to X or five in this case then we add the current value to the right pass and then we return that if neither children contain a pass to the element that we're looking for if we're looking at this element say then we just return no now using the code that we just implemented let's just Implement our main function LCA that takes root and the two elements and let's call them J and K and in particular we'll be looking at this example of the LCA of four and five now the first first thing we do is we find the paths to J and K or four and five which would be these two and then we initialize LCA to return as not or anything else and while pass to J and pass to K are non empty we pop these two stacks or these two paths and call them Jpop and K-pop and popping just means we take the first element and if these two elements JP and KP are the same element then we update LCA to return to that element so LCA to return becomes one and then three because these two elements are still the same and then we stop right here when these two elements J pop and KP are not equal and break and we return LCA to return in this case three as the LCA and that's it for the video thank you so much
Original Description
*Just getting started with coding interviews? Check out my "Get Ready for Your Coding Interview" course on Lynda.com:
https://www.lynda.com/Software-Development-tutorials/How-Ace-Developer-Interview/576698-2.html?lpk35=9181&utm_medium=ldc-partner&utm_source=CMPRC&utm_content=524&utm_campaign=CD20605&bid=524&aid=CD20605
(You'll get a 30-day trial with the link)
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from CS Dojo · CS Dojo · 7 of 60
1
2
3
4
5
6
▶
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
4 Hacks for Finding the Optimal Answer in Coding Interview QUICKLY!
CS Dojo
Dynamic Programming Tutorial with Fibonacci Sequence
CS Dojo
Kadane's Algorithm to Maximum Sum Subarray Problem
CS Dojo
Longest Common Subsequence (Dynamic Programming)
CS Dojo
0-1 Knapsack Problem (Dynamic Programming)
CS Dojo
Amazon Coding Interview: Count Negative Integers in Row/Column-Wise Sorted Matrix
CS Dojo
Microsoft Coding Interview Question and Answer: Lowest Common Ancestor
CS Dojo
Learn Counting Sort Algorithm in LESS THAN 6 MINUTES!
CS Dojo
Radix Sort Algorithm Introduction in 5 Minutes
CS Dojo
Coding Interview Question and Answer: Longest Consecutive Characters
CS Dojo
Coding Interview: Can You RANDOMLY Reorder Array in O(N)?
CS Dojo
Coding Interview Question: Tower Hopper Problem
CS Dojo
Problem Solving Technique #1 for Coding Interviews with Google, Amazon, Microsoft, Facebook, etc.
CS Dojo
Google Coding Interview Question and Answer #1: First Recurring Character
CS Dojo
Facebook Coding Interview Question and Answer #1: All Subsets of a Set
CS Dojo
Think you're not smart enough to work at Google? Well, think again.
CS Dojo
How to Crack a Google Coding Interview - An Ex-Googler’s Guide
CS Dojo
Amazon Coding Interview Question - K Closest Points to the Origin
CS Dojo
How I Got an Internship at Microsoft
CS Dojo
How I Got a Job at Google as a Software Engineer (without a Computer Science Degree!)
CS Dojo
Why I Left My $100,000+ Job at Google
CS Dojo
Top 5 Programming Languages to Learn to Get a Job at Google, Facebook, Microsoft, etc.
CS Dojo
How I Learned to Code - and Got a Job at Google!
CS Dojo
Why I Left Google To Be A YouTuber FULL-TIME (and NOT part-time!)
CS Dojo
What Is Dynamic Programming and How To Use It
CS Dojo
Python Tutorial for Absolute Beginners #1 - What Are Variables?
CS Dojo
What's It Really Like To Intern At Google? (LIVE with a former Google software engineer intern)
CS Dojo
How to Use If Else Statements in Python (Python Tutorial #2)
CS Dojo
Dynamic Programming Interview Question #1 - Find Sets Of Numbers That Add Up To 16
CS Dojo
How To Use Functions In Python (Python Tutorial #3)
CS Dojo
What’s It Like To Be A Program Manager Intern At Microsoft? (LIVE with a former Microsoft intern)
CS Dojo
Introduction To Lists In Python (Python Tutorial #4)
CS Dojo
Introduction to For Loops in Python (Python Tutorial #5)
CS Dojo
What Programming Language Should I Learn First?
CS Dojo
What Is Competitive Programming and How To Prepare For It (LIVE with Gaurav Sen)
CS Dojo
While Loops and The Break Statement in Python (Python Tutorial #6)
CS Dojo
More About For Loops in Python & Solutions to the Last 2 Problems (Python Tutorial #7)
CS Dojo
How to Learn to Code - Best Resources, How to Choose a Project, and more!
CS Dojo
How To Use Dictionaries In Python (Python Tutorial #8)
CS Dojo
Data Structures & Algorithms #1 - What Are Data Structures?
CS Dojo
An Overview of Arrays and Memory (Data Structures & Algorithms #2)
CS Dojo
Introduction to Classes and Objects - Part 1 (Data Structures & Algorithms #3)
CS Dojo
Classes and Objects with Python - Part 1 (Python Tutorial #9)
CS Dojo
Introduction to Classes and Objects - Part 2 (Data Structures & Algorithms #4)
CS Dojo
Classes and Objects with Python - Part 2 (Python Tutorial #10)
CS Dojo
Introduction to Linked Lists (Data Structures & Algorithms #5)
CS Dojo
Introduction to Recursion (Data Structures & Algorithms #6)
CS Dojo
Introduction to Big O Notation and Time Complexity (Data Structures & Algorithms #7)
CS Dojo
Amazon Coding Interview Question - Recursive Staircase Problem
CS Dojo
Using Boolean in Python (Python Tutorial #11)
CS Dojo
Intro to Data Analysis / Visualization with Python, Matplotlib and Pandas | Matplotlib Tutorial
CS Dojo
What Can You Do with Python? - The 3 Main Applications
CS Dojo
Facebook Coding Interview Question - How Many Ways to Decode This Message?
CS Dojo
List Comprehension Basics with Python (Python Tutorial #12)
CS Dojo
How To Use Sets in Python (Python Tutorial #13)
CS Dojo
Python books for beginners? What Python projects to work on? | 2 Python Beginner FAQ’s!
CS Dojo
Resources for Learning Data Structures and Algorithms (Data Structures & Algorithms #8)
CS Dojo
6 Python Exercise Problems for Beginners - from CodingBat (Python Tutorial #14)
CS Dojo
Google Coding Interview - Universal Value Tree Problem
CS Dojo
Best laptops for programming? How to get a job at Google? - And other FAQ’s!
CS Dojo
More on: Algorithm Basics
View skill →Related Reads
📰
📰
📰
📰
How I Built a Free Online Image & PDF Processing Platform with Vue 3 + FastAPI
Dev.to · IAMUU
I Built a Free AI-Powered YouTube SEO Toolkit With Zero Budget. Here’s What Actually Happened.
Medium · Startup
How to Create a Second Version of Yourself Inside Obsidian Using AI (Step-by-Step Guide)
Medium · ChatGPT
How to prepare for Spain civil service TIC exam using AI in 2026
Dev.to · David García
🎓
Tutor Explanation
DeepCamp AI