FastPrepAll Common Ancestors in a Parent Graph
Problems/All Common Ancestors in a Parent Graph

All Common Ancestors in a Parent Graph

Compass International Holdings logoCompass International Holdings● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Compass phone screen. The report asks for all common ancestors. We chose that ancestors must be proper ancestors, what happens when the two nodes are equal or missing, what makes a graph valid, how duplicates are handled, the number order, and the limits. Our version is about 95% the same as the reported question.

Each row of pairs is [parent,child]. Given node IDs a and b, return all their common ancestors.

For this exercise, assume a proper ancestor is a node reached by one or more steps toward parents. A node is not its own ancestor. Equal query IDs are allowed; a query absent from all pairs has no ancestors.

For this exercise, assume relationships form a directed acyclic graph that permits several parents and duplicate pairs have no effect.

For this exercise, assume every common ancestor is returned once in increasing numeric ID order. Return an empty array if there is none. Return all shared generations; do not reduce the answer to lowest common ancestors.

Function

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

Examples

Example 1

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

All common ancestors are returned, including higher generations.

Example 2

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

This query has no shared proper ancestor: root 1 is not its own ancestor.

Constraints

  • For this exercise, assume 0 <= pairs.length <= 1000.
  • Each row contains two positive integer IDs; node and query IDs are at most 10^9.
  • The directed relationships contain no cycles.

More Compass International Holdings Phone Screen questions

  • Decode StringPHONE SCREEN · Seen Oct 2026
  • Lowest Common Ancestors in a Parent DAGPHONE SCREEN · Seen Apr 2022
  • 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
  • Nodes With Zero or Three ParentsPHONE SCREEN · Seen Jan 2021

Similar questions at other companies

  • Task Build Order and File-Change RerunsVanta · PHONE SCREEN
  • Resolve Variable Equations with Dependency ErrorsApplied Intuition · PHONE SCREENApplied Intuition interview questions
  • Nested Substitutions with Cycle DetectionKikoff · PHONE SCREENKikoff interview questions
  • Validate and Resolve Evaluation ChainsPlaid · PHONE SCREEN · ONSITE INTERVIEWPlaid interview questions
  • Route Graph Reachability with a Broken LinkFigma · PHONE SCREENFigma interview questions
  • Root of the Largest TreeGoldman Sachs · PHONE SCREEN · ONSITE INTERVIEWGoldman Sachs interview questions
  • Find the Unique User with Access to Every CameraVerkada · PHONE SCREENVerkada interview questions
  • Unique Transitive Dependent CountsRobinhood · PHONE SCREENRobinhood interview questions
  • Maximum Programmer-Problem MatchingGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
  • Best Currency Round Trip Over Two DaysRippling · PHONE SCREENRippling interview questions
All Compass International Holdings interview questionsSee Compass International Holdings hiring insights
Saved
class Solution {
    public int[] allCommonAncestors(int[][] pairs, int a, int b) {
        // Write your code here.
        return new int[0];
    }
}
Checking account…