FastPrepLargest Tree Size in a Forest
Problems/Largest Tree Size in a Forest

Largest Tree Size in a Forest

Goldman Sachs logoGoldman Sachs● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A Goldman Sachs CoderPad report gives child-parent pairs that describe a forest, and asks for the size of its largest connected component. It does not give the type of a pair that works across languages, what to do with duplicate pairs, or what to return for empty input. Our version is about 96% the same as the reported question.

Each row [child, parent] in relations is a directed edge in a forest. A node may be a parent, a child, or both, but every child has at most one parent and the edges contain no cycle.

Return the number of distinct nodes in the largest tree. Return 0 when relations is empty.

Function

largestTreeSize(relations: int[][]) → int

Examples

Example 1

relations = [[2,1],[3,1],[5,4]]return = 3

Nodes 1, 2, and 3 form the largest tree.

Example 2

relations = [[2,1],[3,2],[4,3],[6,5]]return = 4

The chain containing nodes 1 through 4 has four nodes.

Constraints

  • 0 <= relations.length <= 200000
  • Every row contains two signed 32-bit integer node labels.
  • Pairs are distinct, every child has at most one parent, and the graph is acyclic.

More Goldman Sachs Phone Screen questions

  • House Robber IIIPHONE SCREEN · Seen Nov 2026
  • Trapping Rain WaterPHONE SCREEN · Seen Nov 2026
  • Best Average Student ScorePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Check a Complete Binary TreePHONE SCREEN · Seen Oct 2026
  • Meeting RoomsPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Count Staircase WaysPHONE SCREEN · Seen Oct 2026
  • Is a Power of TenPHONE SCREEN · Seen Oct 2026
  • Minimum Traveling Salesperson TourPHONE SCREEN · Seen Oct 2026
  • Square Root with Newton IterationPHONE SCREEN · Seen Oct 2026
  • First Non-Repeating CharacterPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026

Similar questions at other companies

  • Record Linkage Part 3 - Full Connected ComponentStripe · PHONE SCREENStripe interview questions
  • Group Sparse Points by Distance ThresholdNuro · PHONE SCREENNuro interview questions
  • Sentence Equivalence with SynonymsRead AI · PHONE SCREEN
  • Accounts MergeOracle · ONSITE INTERVIEWOracle interview questions
  • Group Attribute-Connected TripletsGoogle · ONSITE INTERVIEWGoogle interview questions
  • Count Distinct Word MeaningsDuolingo · OADuolingo interview questions
  • Disaster RecoveryHeadlands Technologies · OAHeadlands Technologies interview questions
  • Merge Event PreferencesAirbnb · PHONE SCREEN · ONSITE INTERVIEWAirbnb interview questions
  • Mutual-Friend Referral RecommendationsFireworks AI · PHONE SCREEN · ONSITE INTERVIEW
  • Mutual-Friend RecommendationsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
All Goldman Sachs interview questionsSee Goldman Sachs hiring insights
Saved
class Solution {
    public int largestTreeSize(int[][] relations) {
      // write your code here
    }
}
Checking account…