Largest Tree Size in a Forest
Updated
Problem statement
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[][]) → intExamples
Example 1
relations = [[2,1],[3,1],[5,4]]return = 3Nodes 1, 2, and 3 form the largest tree.
Example 2
relations = [[2,1],[3,2],[4,3],[6,5]]return = 4The 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