Aggregate Investment Ownership from CSV
Reported by candidates from Carta's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Carta OA reported in October 2019 hands you headerless CSV rows and asks for a compact JSON string with ownership as a reduced fraction like "3/4". It's a hash-map aggregation problem wearing a formatting costume. The logic is easy. The output has to match character for character, and that's where people lose points. If you blank on the string building or the fraction reduction, StealthCoder is a safety net that runs invisibly during the live OA and gives you a working solution. Know the shape before you open the timer.
The problem
Each input row is a headerless CSV investment record: date,investorName,sharesPurchased,cashUsed.
Aggregate shares and cash by investor. Ownership is the investor's total shares divided by all purchased shares, reduced to a fraction. Return a canonical compact JSON array sorted by investor name, with each object using the field order investorName, sharesPurchased, cashUsed, ownership.
Function
aggregateOwnership(csvRows: String[]) → String
Examples
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 owns 150 of 200 shares and Bob owns 50.
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\"}]"
Output is sorted by investor, not input order.
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.Reported by candidates. Source: FastPrep
Pattern and pitfall
Split each row on commas. Use a hash map from investor name to running totals of shares and cash. Add every row's shares to a global total as you go. Sort the names, then for each one compute g = gcd(shares, total) and emit shares/g and total/g as a string. Pitfalls: the output is one string, not an array, so build it by hand with no spaces and the exact field order investorName, sharesPurchased, cashUsed, ownership. Ownership is a quoted string, but the shares and cash are bare numbers. Sort by name with plain string comparison, not by input order. Use 64-bit integers, since totals can be large. A single investor gives "1/1", so don't special-case it. If you freeze on the formatting, StealthCoder can give you the reference solution during the live OA. Names have no commas or quotes, so you can skip escaping.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Aggregate Investment Ownership from CSV cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderYou've seen the question.
Make sure you actually pass Carta's OA.
Carta reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Aggregate Investment Ownership from CSV FAQ
How hard is the Carta aggregate ownership problem really?+
Easy on logic, picky on output. It's a map, a sort, and a gcd. The risk is one stray space or wrong field order in the JSON string, which fails every test. Write the formatter carefully and check it against the three examples.
What's the trick to the ownership fraction?+
Sum all shares first, then for each investor divide their shares and the grand total by gcd(shares, total). Print as "num/den" in quotes. Don't compute decimals. Even a sole investor prints "1/1", which falls out of gcd naturally.
Do I need a JSON library for the output?+
Not necessarily, and hand-building is often safer. Names contain no commas or quotes, so you can concatenate directly. Use no whitespace and keep the order investorName, sharesPurchased, cashUsed, ownership. If you use a library, confirm it keeps the compact format and field order.
How should I sort the investors?+
Sort by investor name, not input order. Example 3 shows Amy before Zed even though Zed came first. Use standard string comparison on the names. Names can include digits, spaces, underscores, and hyphens, so rely on the language's default ordering.
How do I prepare for this in 48 hours?+
Practice a small CSV parse, hash-map aggregation, and gcd reduction, then write the string builder from memory. Test the three examples exactly. Also check that you use 64-bit ints, since totals can be large. That covers everything this problem tests.