FastPrepLowest Common Ancestors in a Parent DAG
Problems/Lowest Common Ancestors in a Parent DAG

Lowest Common Ancestors in a Parent DAG

Compass International Holdings logoCompass International Holdings● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Compass phone screen. Our version is about 70% the same as the reported question about lowest common ancestors in a graph. The report does not give its exact graph model or extra twists. We chose a parent DAG, that a node is its own ancestor, how to handle the queries, returning all lowest ancestors in sorted order, and the limits. We wrote all of the complete examples. We do not claim any extension the report does not state.

Each pair in pairs is [parent,child]. Return the lowest common ancestors of a and b in that directed graph.

For this exercise, assume the graph is acyclic and may give a node multiple parents. A node is an ancestor of any node it can reach by following parent-to-child edges. Following zero edges is allowed, so every represented node is its own ancestor. Only nodes appearing in a pair are represented; if either query is absent, return an empty array.

A common ancestor reaches both queries. It is lowest when no different common ancestor is its descendant. For this exercise, assume every lowest common ancestor is returned once in increasing numeric order. There may be several lowest common ancestors, with none of them an ancestor of another. Equal represented queries are allowed, and duplicate pairs do not change the result.

Function

lowestCommonAncestors(pairs: int[][], a: int, b: int) → int[]

Examples

Example 1

pairs = [[1,2],[2,3],[2,4],[4,5]]a = 3b = 5return = [2]

This graph has common ancestors 1 and 2; only 2 is lowest.

Example 2

pairs = [[1,3],[2,3],[1,4],[2,4]]a = 3b = 4return = [1,2]

This DAG has two incomparable lowest common ancestors, so both are returned.

Example 3

pairs = [[9,5],[5,2]]a = 5b = 2return = [5]

This example applies the declared reflexive rule: a query may be its own ancestor, so 5 is lowest.

Constraints

  • For this exercise, assume 0 <= pairs.length <= 500 and at most 200 distinct labels occur in pairs.
  • Every row has exactly two integers. For this exercise, assume all labels and query IDs are from 0 through 1000000.
  • For this exercise, assume there is no directed cycle; pairs may be unordered or repeated.

More Compass International Holdings Phone Screen questions

  • Decode StringPHONE SCREEN · Seen Oct 2026
  • Can One Dictionary Word Supply the Target Letters?PHONE SCREEN · Seen Apr 2022
  • Greedy Grid Word Occurrences with No Reused CellsPHONE SCREEN · Seen Apr 2022
  • Word Path Moving Only Right or DownPHONE SCREEN · Seen Apr 2022
  • First Dictionary Word from Note LettersPHONE SCREEN · Seen Mar 2022
  • Word Path Moving Only Up or RightPHONE SCREEN · Seen Feb 2022
  • Top Five Stock Records in a Date RangePHONE SCREEN · Seen Oct 2021
  • Wrap Words from Existing Text LinesPHONE SCREEN · Seen Apr 2021
  • All Common Ancestors in a Parent GraphPHONE SCREEN · Seen Jan 2021
  • Nodes With Zero or Three ParentsPHONE SCREEN · Seen Jan 2021

Similar questions at other companies

  • Course ScheduleAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Linked Merchants Within Two HopsStripe · PHONE SCREEN
  • Shortest Pin Path Through TopicsPinterest · PHONE SCREEN
  • Shortest Travel Time in a Grid with Traffic LightsWaymo · PHONE SCREEN
  • Word Chain Jumps in a GridGoogle · PHONE SCREEN
  • Evaluate DivisionApple · PHONE SCREEN
  • Shortest Word Chain Between Two WordsFanatics · PHONE SCREEN
  • Minimize CommuteDatabricks · OA · PHONE SCREEN
  • Crawl Same-Hostname URLs ConcurrentlyAnthropic · PHONE SCREEN
  • Possible BipartitionByteDance · PHONE SCREEN
See Compass International Holdings hiring insights
Saved
class Solution {
    public int[] lowestCommonAncestors(int[][] pairs, int a, int b) {
        // Write your code here.
        return new int[0];
    }
}
Checking account…