Capital Gains Tax from Trades
Reported by candidates from Arcesium's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Arcesium OA reported in February 2021 hides its trick in the input size. Shares are positive integers with no stated cap, so expanding every share into its own lot will blow up the moment a trade says 10 million shares. This is a FIFO lot-matching problem with queues per ticker, and it supports shorts too. If the matching logic slips under pressure, StealthCoder is the invisible safety net that reads the problem on screen and hands you a working solution. Know the shape first, though. It's simulation with a queue of (shares, price) lots per ticker.
The problem
Process chronological stock trades and calculate the tax on realized profit. Each trade is a comma-separated string: date,ticker,side,shares,price side is B for a buy or S for a sell. shares is a positive integer. price is a non-negative decimal amount with at most two digits after the decimal point. Track positions separately by ticker and match closing shares against open lots in first-in, first-out order. A sell closes long shares before opening a short position; a buy closes short shares before opening a long position. A single trade may close an existing position and open the opposite position with its remaining shares. For a closed long share, realized profit is sell price - buy price. For a closed short share, realized profit is short-sale price - cover price. Open lots do not contribute realized profit. Add realized profit across every ticker. Tax is 25% of the positive total; when the total is zero or negative, tax is zero. Return the tax as US currency with a leading $, comma separators, and exactly two decimal places. Function calculateTax(trades: String[]) → String Examples Example 1 trades = ["2024-01-01,ABC,B,10,100.00","2024-01-02,ABC,S,4,120.00"] return = "$20.00" Selling four shares realizes 4 * (120 - 100) = $80. Tax is 25%, or $20.00. Example 2 trades = ["2024-01-01,ABC,S,5,50.00","2024-01-02,ABC,B,5,60.00"] return = "$0.00" Covering the short realizes a loss of 5 * (50 - 60) = -$50, so the tax is zero. Constraints trades.length >= 1. Trades are ordered chronologically. Every trade uses the five-field format described above. Every testcase produces a tax amount that is an exact number of cents.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Keep a hash map from ticker to a deque of open lots, each holding remaining shares and price. A positive sign means long, negative means short, and one deque never holds both. On a trade, check the opposite side. While the trade has shares left and the deque has opposite lots, match min(remaining, lot shares), add profit, and shrink or pop the lot. Any leftover shares push a new lot on the trade's side. That handles the flip from long to short in one trade. Profit for a long close is sell minus buy, for a short close it's short price minus cover price. Don't use floats. Parse prices into integer cents, sum in a long, then tax is total/4 when positive. The guarantee of exact cents means no rounding. Format with commas and two decimals. StealthCoder is your hedge in the live OA if the partial-lot logic or the formatting trips you up.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Capital Gains Tax from Trades 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 by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Arcesium's OA.
Arcesium reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Capital Gains Tax from Trades FAQ
What's the trick in the Arcesium capital gains tax problem?+
Per-ticker FIFO queues of lots, matched in bulk instead of share by share. Each lot stores a share count and price, so a trade closes lots with min() arithmetic. Leftover shares after closing open a new lot on the opposite side, which covers the long-to-short flip.
Why shouldn't I use floating point for prices?+
Prices have up to two decimals and the output must be exact cents. Floats can drift and print the wrong cent. Parse each price into integer cents, do all profit math in a 64-bit integer, and only format to dollars at the end.
How do shorts work in this problem?+
A sell with no long lots opens a short lot at the sale price. A later buy closes short lots first, with profit equal to short price minus cover price. A sell closes longs before opening a short, and a buy closes shorts before opening a long.
How do I format the output as currency?+
Take total cents, compute tax as cents divided by 4 when positive, else 0. Split into dollars and cents, insert commas into the dollars every three digits, and pad cents to two digits. Prefix with $. Zero tax prints $0.00.
How do I prepare in 48 hours for this kind of OA?+
Write this one from scratch twice with a map of deques, then test the edge cases: a trade that flips position, multiple tickers, a net loss, and huge share counts. Practice integer-cents parsing and comma formatting in your language so neither costs you time.