Team Cost Under a Manager
Updated
Problem statement
A company keeps its org chart as a flat list. Person i has the id ids[i], a monthly cost costs[i], and a manager whose id is managerIds[i]. A top-level person has no manager, which is written as -1. There can be more than one top-level person, and a manager may appear later in the list than the people who report to them.
The team of a person is that person plus everyone below them: their direct reports, the reports of those reports, and so on.
For each id in queries, return the total monthly cost of that person's team. Return the answers in the same order as queries.
Function
teamCosts(ids: int[], managerIds: int[], costs: int[], queries: int[]) → long[]Examples
Example 1
ids = [4,7,3,9,12]managerIds = [3,-1,7,7,3]costs = [5,50,20,30,8]queries = [3,7,12]return = [33,113,8]Person 7 is the only top-level person. People 3 and 9 report to 7, and people 4 and 12 report to 3. Person 4 is listed before their manager 3. The team of 3 is 3, 4 and 12: 20 + 5 + 8 = 33. The team of 7 is everyone: 50 + 20 + 30 + 5 + 8 = 113. Person 12 has no reports, so the team is just 12: 8.
Example 2
ids = [1,2,3]managerIds = [-1,-1,2]costs = [10,0,6]queries = [2,1,2]return = [6,10,6]There are two top-level people, 1 and 2. Person 3 reports to 2. The team of 2 costs 0 + 6 = 6, and the team of 1 is just 1, costing 10. The same id can be asked twice.
Example 3
ids = [30,20,10]managerIds = [20,10,-1]costs = [10000000,10000000,10000000]queries = [10,20]return = [30000000,20000000]The list goes from the bottom of the chain to the top: 30 reports to 20, and 20 reports to 10. The team of 10 is all three people: 3 × 10^7 = 30000000. The team of 20 is 20 and 30: 20000000.
Constraints
1 <= ids.length <= 2000, andmanagerIdsandcostshave the same length asids1 <= ids[i] <= 10^5, and all ids are different.- Each
managerIds[i]is-1or the id of another person inids, and going up from any person through their managers always ends at a top-level person (there are no loops). 0 <= costs[i] <= 10^71 <= queries.length <= 200, and every query is one of the ids inids.
More Rippling Phone Screen questions
- Filter, Sort, and Deduplicate Scheduled TasksPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Stateful Log Transform and SearchPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Dependency-Aware Task SchedulerPHONE SCREEN · Seen Nov 2026
- Delivery Cost Tracker with Effective-Dated RatesPHONE SCREEN · Seen Nov 2026
- Camel CardsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best and Worst Camel Card CompletionsPHONE SCREEN · Seen Nov 2026
- Corporate Card Expense RulesOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- In-Memory Priority Task SchedulerPHONE SCREEN · Seen Oct 2026
- Limit an Organization Tree's HeightPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Median Across N Sorted ArraysPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Transform and Prune a Mode-Valued Binary TreeGoogle · PHONE SCREENGoogle interview questions
- Top View of a Binary TreeInMobi · ONSITE INTERVIEWInMobi interview questions
- N-ary Index-Path Encoder and DecoderBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Account Roles With Parent-User InheritanceStripe · PHONE SCREENStripe interview questions
- Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
- Validate a BST with a Custom ComparisonApple · PHONE SCREENApple interview questions
- Tree Height After Child PromotionSnowflake · PHONE SCREENSnowflake interview questions
- Binary Tree Spiral Level OrderAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Check a Complete Binary TreeGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
- Route Graph Reachability with a Broken LinkFigma · PHONE SCREENFigma interview questions
Interview reports that mention this question
Rippling interview experiences
- Rippling No-Level Older Reports: 31 Experiences (Part 6 of 6)
- Rippling No-Level Onsite Loops: 24 Experiences (Part 5 of 6)
- Rippling No-Level Screens and Loops: 18 Experiences (Part 4 of 6)
- Rippling No-Level Coding Screens: 37 Experiences (Part 3 of 6)
- Rippling No-Level Design, Screens: 30 Experiences (Part 2 of 6)