PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Atlassian

Find LCA in a tree and extend to a DAG

Last updated: Mar 29, 2026

Quick Overview

This question evaluates proficiency in graph algorithms and data structures, focusing on lowest common ancestor computation in rooted trees and its generalization to directed acyclic graphs, along with complexity analysis for preprocessing and handling many queries.

  • medium
  • Atlassian
  • Coding & Algorithms
  • Machine Learning Engineer

Find LCA in a tree and extend to a DAG

Company: Atlassian

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

## Part A — LCA variant in a rooted tree You are given a rooted tree with `n` nodes labeled `0..n-1`. Input: - `parent[i]` for each node `i` (`parent[root] = -1`). - Two nodes `u` and `v`. Task: - Return the **lowest common ancestor (LCA)** of `u` and `v`. Clarifications: - The LCA is the common ancestor with the greatest depth. - The input always forms a valid tree. Follow-up (variant): - Answer **multiple queries** `q` (up to ~1e5) for different pairs `(u, v)` efficiently. ## Part B — Follow-up: generalize to a DAG Now the structure is a **directed acyclic graph (DAG)** where edges point from a node to its **parent(s)** (a node may have multiple parents). Input: - `parents[i]`: list of parent nodes of `i` (possibly empty). - Two nodes `u` and `v`. Task: - Return the set of **lowest common ancestors** of `u` and `v` in the DAG, defined as: - Nodes that are ancestors of both `u` and `v`, and - Do **not** have any descendant (within the common-ancestor set) that is also a common ancestor. Notes: - In a DAG, there may be **multiple** lowest common ancestors. ## Constraints (reasonable interview constraints) - `n` up to ~2e5, edges up to ~5e5. - DAG is guaranteed acyclic. - Aim for near-linear preprocessing for many queries when applicable.

Quick Answer: This question evaluates proficiency in graph algorithms and data structures, focusing on lowest common ancestor computation in rooted trees and its generalization to directed acyclic graphs, along with complexity analysis for preprocessing and handling many queries.

Related Interview Questions

  • Find a secret word using match feedback - Atlassian (hard)
  • Compute a moving average on a stream - Atlassian (hard)
  • Implement sequential and parallel URL requests - Atlassian (medium)
  • Implement sliding-window rate limiter function - Atlassian (medium)
  • Merge intervals and design rating APIs - Atlassian (medium)
Atlassian logo
Atlassian
Oct 11, 2025, 12:00 AM
Machine Learning Engineer
Onsite
Coding & Algorithms
1
0
Loading...

Part A — LCA variant in a rooted tree

You are given a rooted tree with n nodes labeled 0..n-1.

Input:

  • parent[i] for each node i ( parent[root] = -1 ).
  • Two nodes u and v .

Task:

  • Return the lowest common ancestor (LCA) of u and v .

Clarifications:

  • The LCA is the common ancestor with the greatest depth.
  • The input always forms a valid tree.

Follow-up (variant):

  • Answer multiple queries q (up to ~1e5) for different pairs (u, v) efficiently.

Part B — Follow-up: generalize to a DAG

Now the structure is a directed acyclic graph (DAG) where edges point from a node to its parent(s) (a node may have multiple parents).

Input:

  • parents[i] : list of parent nodes of i (possibly empty).
  • Two nodes u and v .

Task:

  • Return the set of lowest common ancestors of u and v in the DAG, defined as:
    • Nodes that are ancestors of both u and v , and
    • Do not have any descendant (within the common-ancestor set) that is also a common ancestor.

Notes:

  • In a DAG, there may be multiple lowest common ancestors.

Constraints (reasonable interview constraints)

  • n up to ~2e5, edges up to ~5e5.
  • DAG is guaranteed acyclic.
  • Aim for near-linear preprocessing for many queries when applicable.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Atlassian•More Machine Learning Engineer•Atlassian Machine Learning Engineer•Atlassian Coding & Algorithms•Machine Learning Engineer Coding & Algorithms
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
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.