The Capital Gain/Loss interview question (alternate variation) focuses on calculating the total profit or loss from stock transactions. You are given a table recording stock names, whether the action was a "Buy" or "Sell," and the price at which the action occurred. The goal is to return a summary table showing each stock and its net capital gain or loss. This Capital Gain/Loss coding problem is a fundamental task for data analysis in financial applications.
Companies like Robinhood ask this to ensure candidates can work with transaction logs. It requires transforming transaction-level data into a summary view. This problem specifically tests your ability to handle non-numerical labels (Buy/Sell) and perform arithmetic across rows that represent different types of financial events.
This follows the Database interview pattern.
If you bought "Google" for 1000 and sold it for 1200, then bought it again for 1100 and sold it for 1300:
Be comfortable with the "Pivoting" concept in SQL. While this isn't a full pivot, it uses the same logic of using conditional statements inside aggregate functions to process different categories of data into a single numerical result.
| 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 |