Problem · Tree

Group Loans by Top-Level Parent Company

Learn this problem
MediumAffirm logoAffirmFULLTIMEPHONE SCREEN

Problem statement

For each loan, follow the borrowing company's parent chain to its root company. Return one [rootIndex,totalAmount] row for each root receiving a loan.

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]]

Companies one and zero roll up to root zero; company three rolls up to root two.

Example 2

parents = [-1]loanCompanies = [0,0]amounts = [4,6]return = [[0,10]]

Multiple loans for one root are summed.

Constraints

  • 1 <= parents.length <= 100000
  • parents[i] == -1 or 0 <= parents[i] < i.
  • Loan company and amount arrays have equal length.
  • Amounts are nonnegative and totals fit signed 64-bit integers.

More Affirm problems

drafts saved locally
public long[][] groupLoans(int[] parents, int[] loanCompanies, long[] amounts) {
  // Write your code here.
}
parents[-1,0,-1,2]
loanCompanies[1,3,0]
amounts[5,7,2]
expected[[0,7],[2,7]]
checking account