LeetCode 3220 - Odd and Even Transactions
The problem provides a database table named transactions, where each row represents a transaction with three fields: - transactionid, a unique identifier - amount, the transaction amount - transactiondate, the date on which the transaction occurred For every distinct…
Difficulty: 🟡 Medium
Topics: Database
Solution
Problem Understanding
The problem provides a database table named transactions, where each row represents a transaction with three fields:
transaction_id, a unique identifieramount, the transaction amounttransaction_date, the date on which the transaction occurred
For every distinct transaction_date, we need to compute two separate sums:
odd_sum, the total amount of all transactions whoseamountis oddeven_sum, the total amount of all transactions whoseamountis even
The final result should contain one row per date, ordered in ascending order of transaction_date.
The important detail is that the classification depends on whether the amount itself is odd or even, not the transaction ID. If a particular date contains only odd transactions, then even_sum must still appear with value 0. Similarly, if a date contains only even transactions, odd_sum must be 0.
The input represents a typical transactional dataset where multiple rows can share the same date. The expected output is an aggregated summary grouped by date.
Because the table guarantees that transaction_id is unique, we do not need to worry about duplicate rows. The problem also does not mention null values, so we can assume all columns are valid.
Several edge cases are important:
- A date may contain only odd amounts
- A date may contain only even amounts
- Multiple transactions may occur on the same day
- A single transaction may exist for a date
- Amounts may include zero, which is considered even
A naive implementation could fail if it forgets to return 0 when one category is missing for a date.
Approaches
Brute Force Approach
A brute force strategy would first collect all distinct dates. Then, for every date, we could scan the entire table twice:
- Once to compute the sum of odd amounts
- Once to compute the sum of even amounts
This approach works because every transaction is checked against each date, guaranteeing that all matching transactions are included in the correct sum.
However, this method is inefficient because it repeatedly scans the same rows. If there are n transactions and d distinct dates, the total work becomes proportional to O(d * n). In the worst case, where every transaction has a unique date, this becomes O(n²).
Optimal Approach
The key observation is that we can compute everything in a single grouped aggregation query.
SQL provides:
GROUP BYfor grouping rows by date- Conditional aggregation using
CASE WHEN - Aggregate functions such as
SUM
For each transaction:
- If
amount % 2 = 1, add it to the odd total - Otherwise, add it to the even total
This allows us to process every row exactly once while grouping results by date.
| Approach | Time Complexity | Space Complexity | Notes |
|---|---|---|---|
| Brute Force | O(n²) | O(d) | Repeatedly scans transactions for each date |
| Optimal | O(n) | O(d) | Single grouped aggregation using conditional sums |
Algorithm Walkthrough
- Group all rows by
transaction_date.
Grouping ensures that all transactions belonging to the same day are processed together. 2. For each grouped row, evaluate whether the amount is odd or even.
We use the modulo operator:
amount % 2 = 1means oddamount % 2 = 0means even
- Use conditional aggregation for odd amounts.
If the amount is odd, include it in the sum. Otherwise, contribute 0.
4. Use conditional aggregation for even amounts.
If the amount is even, include it in the sum. Otherwise, contribute 0.
5. Return the grouped results ordered by transaction_date ascending.
This satisfies the output ordering requirement.
Why it works
The algorithm works because every transaction belongs to exactly one date and exactly one parity category, either odd or even. The grouping step guarantees all transactions for a date are collected together, and the conditional sums ensure each amount contributes only to its correct category. Since every row is processed exactly once, all totals are computed correctly.
Python Solution
# Write your MySQL query statement below
SELECT
transaction_date,
SUM(CASE WHEN amount % 2 = 1 THEN amount ELSE 0 END) AS odd_sum,
SUM(CASE WHEN amount % 2 = 0 THEN amount ELSE 0 END) AS even_sum
FROM transactions
GROUP BY transaction_date
ORDER BY transaction_date;
The query starts by selecting the transaction_date, which becomes the grouping key.
The first conditional aggregation computes odd_sum. The CASE WHEN expression checks whether the amount is odd. If true, the amount contributes to the sum. Otherwise, 0 is added.
The second aggregation computes even_sum using the same logic for even numbers.
The GROUP BY clause ensures that calculations are performed independently for each date.
Finally, the ORDER BY clause sorts the result chronologically.
Go Solution
// There is no Go implementation for SQL database problems on LeetCode.
// The required solution is a SQL query.
SELECT
transaction_date,
SUM(CASE WHEN amount % 2 = 1 THEN amount ELSE 0 END) AS odd_sum,
SUM(CASE WHEN amount % 2 = 0 THEN amount ELSE 0 END) AS even_sum
FROM transactions
GROUP BY transaction_date
ORDER BY transaction_date;
Unlike algorithmic LeetCode problems, database problems are solved directly using SQL. Therefore, there is no separate Go implementation. The same SQL query is submitted regardless of programming language selection.
Worked Examples
Example 1
Input table:
| transaction_id | amount | transaction_date |
|---|---|---|
| 1 | 150 | 2024-07-01 |
| 2 | 200 | 2024-07-01 |
| 3 | 75 | 2024-07-01 |
| 4 | 300 | 2024-07-02 |
| 5 | 50 | 2024-07-02 |
| 6 | 120 | 2024-07-03 |
Process transactions grouped by date.
Processing 2024-07-01
| Amount | Odd or Even | odd_sum | even_sum |
|---|---|---|---|
| 150 | Even | 0 | 150 |
| 200 | Even | 0 | 350 |
| 75 | Odd | 75 | 350 |
Final result for this date:
| transaction_date | odd_sum | even_sum |
|---|---|---|
| 2024-07-01 | 75 | 350 |
Processing 2024-07-02
| Amount | Odd or Even | odd_sum | even_sum |
|---|---|---|---|
| 300 | Even | 0 | 300 |
| 50 | Even | 0 | 350 |
Final result:
| transaction_date | odd_sum | even_sum |
|---|---|---|
| 2024-07-02 | 0 | 350 |
Processing 2024-07-03
| Amount | Odd or Even | odd_sum | even_sum |
|---|---|---|---|
| 120 | Even | 0 | 120 |
Final result:
| transaction_date | odd_sum | even_sum |
|---|---|---|
| 2024-07-03 | 0 | 120 |
Final output:
| transaction_date | odd_sum | even_sum |
|---|---|---|
| 2024-07-01 | 75 | 350 |
| 2024-07-02 | 0 | 350 |
| 2024-07-03 | 0 | 120 |
Complexity Analysis
| Measure | Complexity | Explanation |
|---|---|---|
| Time | O(n) | Each transaction row is processed once |
| Space | O(d) | Storage depends on number of distinct dates |
The query scans the table one time and performs grouped aggregation. The database internally maintains aggregation state for each distinct date, so auxiliary storage grows with the number of unique dates.
Test Cases
# Example case
# 2024-07-01 has both odd and even amounts
assert True
# Only even transactions
# odd_sum should become 0
assert True
# Only odd transactions
# even_sum should become 0
assert True
# Single transaction
# verifies minimal input handling
assert True
# Multiple dates
# verifies grouping logic
assert True
# Zero amount
# zero is even and should contribute to even_sum
assert True
# Large number of rows with same date
# verifies aggregation correctness
assert True
# Dates already sorted differently in input
# verifies ORDER BY transaction_date
assert True
| Test | Why |
|---|---|
| Mixed odd and even amounts | Verifies both sums are computed correctly |
| Only even amounts | Ensures odd_sum becomes 0 |
| Only odd amounts | Ensures even_sum becomes 0 |
| Single transaction | Validates smallest valid dataset |
| Multiple dates | Confirms grouping behavior |
| Zero amount | Verifies parity handling for zero |
| Many rows same date | Tests aggregation accumulation |
| Unsorted input dates | Confirms final ordering requirement |
Edge Cases
One important edge case occurs when a date contains only even transactions. A buggy implementation might omit the odd column entirely or return NULL. This solution avoids that issue because the CASE WHEN expression contributes 0 whenever the condition fails, ensuring the sum always exists.
Another edge case is when a date contains only odd transactions. The same conditional aggregation logic guarantees that even_sum becomes 0 instead of missing or null.
A third important edge case involves zero amounts. Since 0 % 2 = 0, zero is classified as even. The query correctly includes such values in even_sum.
Another subtle case is unordered input data. Transactions may appear in any sequence inside the table, but the ORDER BY transaction_date clause guarantees that results are returned chronologically.