The Capital Gain/Loss interview question is a SQL problem involving stock trades. You are given a Stocks table with columns stock_name, operation ('Buy' or 'Sell'), day, and price. You need to calculate the total capital gain or loss for each stock after all trades. The gain/loss is calculated as the sum of all selling prices minus the sum of all buying prices. This Capital Gain/Loss coding problem tests your ability to perform conditional aggregation.
Robinhood and other fintech firms use this to check if you can handle financial ledger data. It tests your proficiency with SQL GROUP BY and the use of CASE statements or IF functions within a SUM() aggregate. It evaluates how you transform categorical data ('Buy'/'Sell') into numerical weights (+/-).
This follows the Database interview pattern of "Group and Conditional Sum."
Stock "ABC":
When calculating net totals from different types of operations (Credits/Debits, Buys/Sells), always use SUM(CASE ...) to assign positive and negative values. It’s much cleaner and more performant than using multiple joins.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Capital Gain/Loss | Medium | Solve | |
| Active Businesses | Medium | Solve | |
| Active Users | Medium | Solve | |
| Activity Participants | Medium | Solve | |
| All People Report to the Given Manager | Medium | Solve |