Total Balance of an Account and Its Sub-Accounts
Updated
Problem statement
A bank keeps its accounts in a hierarchy. Each row of accounts is [id, parentId, balance]: an account, the account it sits directly under, and the money it holds. The one top-level account has parentId = -1. Rows come in any order.
target is the id of one account. Return the total balance of that account together with all of its sub-accounts, at every depth.
Function
accountTotal(accounts: int[][], target: int) → intExamples
Example 1
accounts = [[8,7,10],[7,100,40],[100,-1,500],[20,64,3],[31,100,25],[64,31,5]]target = 100return = 583Account 100 is the top of the hierarchy, so its total includes every account: 500 + 40 + 25 + 10 + 5 + 3 = 583; account 20 sits three levels down, under 64, under 31.
Example 2
accounts = [[8,7,10],[7,100,40],[100,-1,500],[20,64,3],[31,100,25],[64,31,5]]target = 7return = 50Account 7 has one sub-account, 8. The total is 40 + 10 = 50; the sibling branch under 31 is not included.
Example 3
accounts = [[9,-1,0]]target = 9return = 0The only account has no sub-accounts, so the total is its own balance, 0.
Constraints
1 <= accounts.length <= 10000 <= id, balance <= 10^6, and all ids are different.- Exactly one account has
parentId = -1, every other parent id is the id of an account, andtargetis an id inaccounts.
More Capital One Onsite Interview questions
- Digit-Coded Card ValidationONSITE INTERVIEW · Seen Nov 2026
- Sort ColorsONSITE INTERVIEW · Seen Oct 2026
- Virtual Card Transaction ValidatorONSITE INTERVIEW · Seen Oct 2026
- Banking System with Top ActivityONSITE INTERVIEW · Seen Sep 2026
- Transaction Event StoreONSITE INTERVIEW · Seen Jul 2026
- Shortest Path in an Unweighted GraphONSITE INTERVIEW · Seen Jul 2026
- Duplicate Transactions Within 60 SecondsONSITE INTERVIEW · Seen May 2026
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen May 2026
- Cashback TrackerONSITE INTERVIEW · Seen Nov 2025
- Reverse a StringOA · ONSITE INTERVIEW · Seen Oct 2025
Similar questions at other companies
- N-ary Index-Path Encoder and DecoderBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Delete Nodes and Return Forest RootsGoogle · ONSITE INTERVIEWGoogle interview questions
- JSON Path Lookup with Implicit Array WildcardsMongoDB · ONSITE INTERVIEWMongoDB interview questions
- Merge N-Ary Trees by KeyLinkedIn · PHONE SCREEN · ONSITE INTERVIEWLinkedIn interview questions
- Filesystem Hash TreeCursor · PHONE SCREEN · ONSITE INTERVIEWCursor interview questions
- Render an Org Chart and Find Skip-Level PairsSnap Inc. · ONSITE INTERVIEWSnap Inc. interview questions
- Render a Comment HierarchyAmazon · ONSITE INTERVIEW
- Topmost Accessible NodesFigma · PHONE SCREEN · ONSITE INTERVIEWFigma interview questions
- Outline from Flat Parent RecordsRippling · ONSITE INTERVIEWRippling interview questions
- Top View of a Binary TreeInMobi · ONSITE INTERVIEWInMobi interview questions
Capital One interview experiences
- Capital One Experienced Engineer Power Day and OA: 6 Experiences
- Capital One New Grad OA Questions: 14 Experiences (Part 2 of 2)
- Capital One TDP Agentic Round (AI Coding): 7 Experiences
- Capital One SWE Power Day, Level Not Reported: 6 Experiences
- Capital One TIP Intern OA and Power Day: 13 Experiences
- Capital One TDP Agentic Power Day: 25 Experiences (Part 1 of 2)