MEDIUMasked at 1 company

Capital Gain/Loss

A medium-tier problem at 85% community acceptance, tagged with Database. Reported in interviews at Robinhood and 0 others.

Founder's read

You're staring at a trading scenario. Track buy and sell transactions, calculate gains and losses, maybe join tables or aggregate across time windows. Robinhood asked this. The acceptance rate is 85 percent, which feels high until you hit the real test and realize you've built the query wrong. The trap: thinking transaction-by-transaction instead of matching buys to sells in the right order. If you blank on the grouping logic or the date windowing during your live OA, StealthCoder surfaces the correct query in seconds, invisible to the proctor.

Companies asking
1
Difficulty
MEDIUM
Acceptance
85%

Companies that ask "Capital Gain/Loss"

If this hits your live OA

Capital Gain/Loss is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by an engineer at a top-10 tech company who can solve these problems cold but didn't want to trust himself in a 90-minute screen share.

Get StealthCoder
What this means

This is a database problem disguised as a profit calculation. You're given a table of buy and sell events, and you need to match them up and compute realized gains or losses. The gotcha is determining which shares you sold, FIFO, LIFO, cost basis matching, and aggregating correctly without double-counting. Most candidates write a naive self-join and end up with a Cartesian explosion or mismatched rows. The real solution involves window functions (ROW_NUMBER, LAG/LEAD, or running sums) to pair transactions in order, then SUM the differences. You might also need to handle partial fills or calculate unrealized gains separately. Common failure modes: forgetting to partition by symbol, mixing aggregate and non-aggregate columns, or not accounting for the date sequence. When you're live and the test data doesn't match your output, StealthCoder runs the correct approach instantly, no hesitation.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Capital Gain/Loss recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by an engineer at a top-10 tech company who can solve these problems cold but didn't want to trust himself in a 90-minute screen share. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Capital Gain/Loss interview FAQ

Is this problem really just a SQL join, or is there a trick I'm missing?+

It's not. A simple join won't pair transactions correctly. You need window functions to track transaction order and match buys to sells sequentially. The trick is using ROW_NUMBER or similar to create a synthetic pairing key that respects chronology. Without it, you'll get duplicates or wrong pairings.

Do I need to handle FIFO or LIFO, or does the problem specify?+

Check the problem statement closely. Most versions assume FIFO (first in, first out), which means your window function should order by transaction date ascending. If the problem is silent, FIFO is the safer assumption. Some variants ask for weighted average cost instead.

Why is the acceptance rate so high if this is a Medium?+

The problem statement is usually clear and the test cases are forgiving. Candidates who know window functions solve it fast. Those who don't often brute-force a join and happen to pass small datasets. On a large dataset with overlapping buy-sell pairs, those solutions crater.

Should I calculate realized and unrealized gains separately?+

Only if the problem asks for both. Most versions focus on realized gains (completed buy-sell pairs). Unrealized is stock you still hold. Read the problem definition carefully. If it's ambiguous, ask during the assessment or assume realized only.

What if there are more sells than buys, or vice versa?+

Handle partial fills and open positions. If you sold more than you bought, you shorted. If you bought more than you sold, you're holding. Use a running sum or cumulative balance to track shares on hand. The problem should clarify whether shorts are allowed.

Want the actual problem statement? View "Capital Gain/Loss" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.