Group Loans by Top-Level Parent Company
Updated
Problem statement
You are given a list of companies and a list of loans. Companies are numbered by their index. parents[i] is the parent company of company i. A company with no parent is a root company.
Loan j goes to company loanCompanies[j] and has amount amounts[j]. For each loan, follow the borrowing company's parent chain up to its root company, and add the loan's amount to that root's total.
Return one [rootIndex,totalAmount] row for each root that receives at least one loan. Sort the rows by root index, smallest first.
Function
groupLoans(parents: int[], loanCompanies: int[], amounts: long[]) → long[][]Examples
Example 1
parents = [-1,0,-1,2]loanCompanies = [1,3,0]amounts = [5,7,2]return = [[0,7],[2,7]]The loans to companies one and zero both roll up to root zero, for a total of seven. The loan to company three rolls up through company two, which is a root, so root two also totals seven.
Example 2
parents = [-1]loanCompanies = [0,0]amounts = [4,6]return = [[0,10]]Both loans go to company zero, which is a root. Their amounts add up, so root zero totals ten.
Constraints
1 <= parents.length <= 100000parents[i] == -1(companyihas no parent) or0 <= parents[i] < i.loanCompaniesandamountshave the same length.- Amounts are nonnegative, and every total fits in a signed 64-bit integer.
More Affirm interview questions
- Redeemable Promotion OffersPHONE SCREEN · Seen Dec 2026
- Propagating Fraud DetectorPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Cross-Day User Trust ScoresPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Count Distinct Underwriting PII ValuesPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- High-Card Pot GamePHONE SCREEN · Seen Sep 2026
- Assign Sequential Loan IdentifiersPHONE SCREEN · Seen Sep 2026
- Normalize Loan Merchants to Root BusinessesPHONE SCREEN · Seen Sep 2026
- Group Transactions by HourONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Predicate Expression Tree EvaluatorMongoDB · PHONE SCREENMongoDB interview questions
- Lowest Common Manager for Employee GroupsAmazon · PHONE SCREEN
- Lowest Common Manager for Employee GroupsGoogle · PHONE SCREENGoogle interview questions
- Collatz Conjecture with Shared MemoizationBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- House Robber IIIOkta · PHONE SCREENOkta interview questions
- House Robber IIIGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
- Tree Selection and VisibilityJane Street · PHONE SCREENJane Street interview questions
- Account Roles With Parent-User InheritanceStripe · PHONE SCREENStripe interview questions
- House Robber IIIVisa · PHONE SCREENVisa interview questions