Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using core interview patterns strategy.
Table: Stocks
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| stock_name | varchar |
| operation | enum |
| operation_day | int |
| price | int |
+---------------+---------+
(stock_name, operation_day) is the primary key (combination of columns with unique values) for this table.
The operation column is an ENUM (category) of type ('Sell', 'Buy')
Each row of this table indicates that the stock which has stock_name had an operation on the day operation_day with the price.
It is guaranteed that each 'Sell' operation for a stock has a corresponding 'Buy' operation in a previous day. It is also guaranteed that each 'Buy' operation for a stock has a corresponding 'Sell' operation in an upcoming day.
Write a solution to report the Capital gain/loss for each stock.
The Capital gain/loss of a stock is the total gain or loss after buying and selling the stock one or many times.
Return the result table in any order.
The result format is in the following example.
Example 1:
Input:
Stocks table:
+---------------+-----------+---------------+--------+
| stock_name | operation | operation_day | price |
+---------------+-----------+---------------+--------+
| Leetcode | Buy | 1 | 1000 |
| Corona Masks | Buy | 2 | 10 |
| Leetcode | Sell | 5 | 9000 |
| Handbags | Buy | 17 | 30000 |
| Corona Masks | Sell | 3 | 1010 |
| Corona Masks | Buy | 4 | 1000 |
| Corona Masks | Sell | 5 | 500 |
| Corona Masks | Buy | 6 | 1000 |
| Handbags | Sell | 29 | 7000 |
| Corona Masks | Sell | 10 | 10000 |
+---------------+-----------+---------------+--------+
Output:
+---------------+-------------------+
| stock_name | capital_gain_loss |
+---------------+-------------------+
| Corona Masks | 9500 |
| Leetcode | 8000 |
| Handbags | -23000 |
+---------------+-------------------+
Explanation:
Leetcode stock was bought at day 1 for 1000$ and was sold at day 5 for 9000$. Capital gain = 9000 - 1000 = 8000$.
Handbags stock was bought at day 17 for 30000$ and was sold at day 29 for 7000$. Capital loss = 7000 - 30000 = -23000$.
Corona Masks stock was bought at day 1 for 10$ and was sold at day 3 for 1010$. It was bought again at day 4 for 1000$ and was sold at day 5 for 500$. At last, it was bought at day 6 for 1000$ and was sold at day 10 for 10000$. Capital gain/loss is the sum of capital gains/losses for each ('Buy' --> 'Sell') operation = (1010 - 10) + (500 - 1000) + (10000 - 1000) = 1000 - 500 + 9000 = 9500$.
Problem summary: Table: Stocks +---------------+---------+ | Column Name | Type | +---------------+---------+ | stock_name | varchar | | operation | enum | | operation_day | int | | price | int | +---------------+---------+ (stock_name, operation_day) is the primary key (combination of columns with unique values) for this table. The operation column is an ENUM (category) of type ('Sell', 'Buy') Each row of this table indicates that the stock which has stock_name had an operation on the day operation_day with the price. It is guaranteed that each 'Sell' operation for a stock has a corresponding 'Buy' operation in a previous day. It is also guaranteed that each 'Buy' operation for a stock has a corresponding 'Sell' operation in an upcoming day. Write a solution to report the Capital gain/loss for each stock. The Capital gain/loss of a stock is the total gain or loss after buying and selling the stock one
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
{"headers":{"Stocks":["stock_name","operation","operation_day","price"]},"rows":{"Stocks":[["Leetcode","Buy",1,1000],["Corona Masks","Buy",2,10],["Leetcode","Sell",5,9000],["Handbags","Buy",17,30000],["Corona Masks","Sell",3,1010],["Corona Masks","Buy",4,1000],["Corona Masks","Sell",5,500],["Corona Masks","Buy",6,1000],["Handbags","Sell",29,7000],["Corona Masks","Sell",10,10000]]}}Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1393: Capital Gain/Loss
// Auto-generated Java example from rust.
class Solution {
public void exampleSolution() {
}
}
// Reference (rust):
// // Accepted solution for LeetCode #1393: Capital Gain/Loss
// pub fn sql_example() -> &'static str {
// r#"
// -- Accepted solution for LeetCode #1393: Capital Gain/Loss
// # Write your MySQL query statement below
// SELECT
// stock_name,
// SUM(IF(operation = 'Buy', -price, price)) AS capital_gain_loss
// FROM Stocks
// GROUP BY 1;
// "#
// }
// Accepted solution for LeetCode #1393: Capital Gain/Loss
// Auto-generated Go example from rust.
func exampleSolution() {
}
// Reference (rust):
// // Accepted solution for LeetCode #1393: Capital Gain/Loss
// pub fn sql_example() -> &'static str {
// r#"
// -- Accepted solution for LeetCode #1393: Capital Gain/Loss
// # Write your MySQL query statement below
// SELECT
// stock_name,
// SUM(IF(operation = 'Buy', -price, price)) AS capital_gain_loss
// FROM Stocks
// GROUP BY 1;
// "#
// }
# Accepted solution for LeetCode #1393: Capital Gain/Loss
# Auto-generated Python example from rust.
def example_solution() -> None:
return
# Reference (rust):
# // Accepted solution for LeetCode #1393: Capital Gain/Loss
# pub fn sql_example() -> &'static str {
# r#"
# -- Accepted solution for LeetCode #1393: Capital Gain/Loss
# # Write your MySQL query statement below
# SELECT
# stock_name,
# SUM(IF(operation = 'Buy', -price, price)) AS capital_gain_loss
# FROM Stocks
# GROUP BY 1;
# "#
# }
// Accepted solution for LeetCode #1393: Capital Gain/Loss
pub fn sql_example() -> &'static str {
r#"
-- Accepted solution for LeetCode #1393: Capital Gain/Loss
# Write your MySQL query statement below
SELECT
stock_name,
SUM(IF(operation = 'Buy', -price, price)) AS capital_gain_loss
FROM Stocks
GROUP BY 1;
"#
}
// Accepted solution for LeetCode #1393: Capital Gain/Loss
// Auto-generated TypeScript example from rust.
function exampleSolution(): void {
}
// Reference (rust):
// // Accepted solution for LeetCode #1393: Capital Gain/Loss
// pub fn sql_example() -> &'static str {
// r#"
// -- Accepted solution for LeetCode #1393: Capital Gain/Loss
// # Write your MySQL query statement below
// SELECT
// stock_name,
// SUM(IF(operation = 'Buy', -price, price)) AS capital_gain_loss
// FROM Stocks
// GROUP BY 1;
// "#
// }
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.