Aggregate Investment Ownership from CSV
Updated
Problem statement
Each string in csvRows is one investment record in CSV form, with no header row: date,investorName,sharesPurchased,cashUsed.
Add up the shares and the cash for each investor. An investor's ownership is their total shares divided by all shares bought by everyone. Write it as a fraction in lowest terms, stored as a JSON string.
Return one compact JSON array as a string, with no extra spaces or line breaks. Sort it by investor name. Each object lists its fields in this order: investorName, sharesPurchased, cashUsed, ownership.
Function
aggregateOwnership(csvRows: String[]) → StringExamples
Example 1
csvRows = ["2025-01-01,Alice,100,500","2025-01-02,Bob,50,400","2025-01-03,Alice,50,300"]return = "[{\"investorName\":\"Alice\",\"sharesPurchased\":150,\"cashUsed\":800,\"ownership\":\"3/4\"},{\"investorName\":\"Bob\",\"sharesPurchased\":50,\"cashUsed\":400,\"ownership\":\"1/4\"}]"Alice has two rows, so she owns 150 of the 200 shares, which is three quarters. Bob owns the other 50 shares, which is one quarter. Alice comes first because the array is sorted by name.
Example 2
csvRows = ["2025-01-01,Solo,7,21"]return = "[{\"investorName\":\"Solo\",\"sharesPurchased\":7,\"cashUsed\":21,\"ownership\":\"1/1\"}]"The only investor owns all shares.
Example 3
csvRows = ["2025-01-01,Zed,1,10","2025-01-02,Amy,2,30"]return = "[{\"investorName\":\"Amy\",\"sharesPurchased\":2,\"cashUsed\":30,\"ownership\":\"2/3\"},{\"investorName\":\"Zed\",\"sharesPurchased\":1,\"cashUsed\":10,\"ownership\":\"1/3\"}]"Zed comes first in the input, but Amy comes first in the output because the array is sorted by investor name, not by input order. Amy owns two of the three shares and Zed owns one.
Constraints
1 <= csvRows.length <= 100000.- Dates use
YYYY-MM-DD. Investor names contain ASCII letters, digits, spaces, underscores, or hyphens and contain no comma or quote. - Shares and cash are positive integers; all totals fit signed 64-bit integers.
Similar questions at other companies
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEW
- Group AnagramsWorkday · PHONE SCREEN
- Bank Account Balances (Part 2): Monthly ReconciliationStripe · PHONE SCREEN
- Decode Words with Anagram KeysCresta · OA
- Time-Aware Search AutocompleteRoblox · PHONE SCREEN
- Top Ten Most Frequent Words in a BookRobinhood · PHONE SCREEN
- Group AnagramsEPAM · PHONE SCREEN
- Aggregate Matching IPv4 Addresses Across ResponsesPinterest · PHONE SCREEN
- Longest String ChainVisa · OA
- Diff Two Filesystem Hash TreesCursor · PHONE SCREEN