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: Transactions
+---------------+---------+ | Column Name | Type | +---------------+---------+ | id | int | | country | varchar | | state | enum | | amount | int | | trans_date | date | +---------------+---------+ id is the primary key of this table. The table has information about incoming transactions. The state column is an enum of type ["approved", "declined"].
Write an SQL query to find for each month and country, the number of transactions and their total amount, the number of approved transactions and their total amount.
Return the result table in any order.
The query result format is in the following example.
Example 1:
Input: Transactions table: +------+---------+----------+--------+------------+ | id | country | state | amount | trans_date | +------+---------+----------+--------+------------+ | 121 | US | approved | 1000 | 2018-12-18 | | 122 | US | declined | 2000 | 2018-12-19 | | 123 | US | approved | 2000 | 2019-01-01 | | 124 | DE | approved | 2000 | 2019-01-07 | +------+---------+----------+--------+------------+ Output: +----------+---------+-------------+----------------+--------------------+-----------------------+ | month | country | trans_count | approved_count | trans_total_amount | approved_total_amount | +----------+---------+-------------+----------------+--------------------+-----------------------+ | 2018-12 | US | 2 | 1 | 3000 | 1000 | | 2019-01 | US | 1 | 1 | 2000 | 2000 | | 2019-01 | DE | 1 | 1 | 2000 | 2000 | +----------+---------+-------------+----------------+--------------------+-----------------------+
Problem summary: Table: Transactions +---------------+---------+ | Column Name | Type | +---------------+---------+ | id | int | | country | varchar | | state | enum | | amount | int | | trans_date | date | +---------------+---------+ id is the primary key of this table. The table has information about incoming transactions. The state column is an enum of type ["approved", "declined"]. Write an SQL query to find for each month and country, the number of transactions and their total amount, the number of approved transactions and their total amount. Return the result table in any order. The query result format is in the following example.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
{"headers":{"Transactions":["id","country","state","amount","trans_date"]},"rows":{"Transactions":[[121,"US","approved",1000,"2018-12-18"],[122,"US","declined",2000,"2018-12-19"],[123,"US","approved",2000,"2019-01-01"],[124,"DE","approved",2000,"2019-01-07"]]}}monthly-transactions-ii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1193: Monthly Transactions I
// Auto-generated Java example from rust.
class Solution {
public void exampleSolution() {
}
}
// Reference (rust):
// // Accepted solution for LeetCode #1193: Monthly Transactions I
// pub fn sql_example() -> &'static str {
// r#"
// -- Accepted solution for LeetCode #1193: Monthly Transactions I
// # Write your MySQL query statement below
// SELECT
// DATE_FORMAT(trans_date, '%Y-%m') AS month,
// country,
// COUNT(1) AS trans_count,
// SUM(state = 'approved') AS approved_count,
// SUM(amount) AS trans_total_amount,
// SUM(IF(state = 'approved', amount, 0)) AS approved_total_amount
// FROM Transactions
// GROUP BY 1, 2;
// "#
// }
// Accepted solution for LeetCode #1193: Monthly Transactions I
// Auto-generated Go example from rust.
func exampleSolution() {
}
// Reference (rust):
// // Accepted solution for LeetCode #1193: Monthly Transactions I
// pub fn sql_example() -> &'static str {
// r#"
// -- Accepted solution for LeetCode #1193: Monthly Transactions I
// # Write your MySQL query statement below
// SELECT
// DATE_FORMAT(trans_date, '%Y-%m') AS month,
// country,
// COUNT(1) AS trans_count,
// SUM(state = 'approved') AS approved_count,
// SUM(amount) AS trans_total_amount,
// SUM(IF(state = 'approved', amount, 0)) AS approved_total_amount
// FROM Transactions
// GROUP BY 1, 2;
// "#
// }
# Accepted solution for LeetCode #1193: Monthly Transactions I
# Auto-generated Python example from rust.
def example_solution() -> None:
return
# Reference (rust):
# // Accepted solution for LeetCode #1193: Monthly Transactions I
# pub fn sql_example() -> &'static str {
# r#"
# -- Accepted solution for LeetCode #1193: Monthly Transactions I
# # Write your MySQL query statement below
# SELECT
# DATE_FORMAT(trans_date, '%Y-%m') AS month,
# country,
# COUNT(1) AS trans_count,
# SUM(state = 'approved') AS approved_count,
# SUM(amount) AS trans_total_amount,
# SUM(IF(state = 'approved', amount, 0)) AS approved_total_amount
# FROM Transactions
# GROUP BY 1, 2;
# "#
# }
// Accepted solution for LeetCode #1193: Monthly Transactions I
pub fn sql_example() -> &'static str {
r#"
-- Accepted solution for LeetCode #1193: Monthly Transactions I
# Write your MySQL query statement below
SELECT
DATE_FORMAT(trans_date, '%Y-%m') AS month,
country,
COUNT(1) AS trans_count,
SUM(state = 'approved') AS approved_count,
SUM(amount) AS trans_total_amount,
SUM(IF(state = 'approved', amount, 0)) AS approved_total_amount
FROM Transactions
GROUP BY 1, 2;
"#
}
// Accepted solution for LeetCode #1193: Monthly Transactions I
// Auto-generated TypeScript example from rust.
function exampleSolution(): void {
}
// Reference (rust):
// // Accepted solution for LeetCode #1193: Monthly Transactions I
// pub fn sql_example() -> &'static str {
// r#"
// -- Accepted solution for LeetCode #1193: Monthly Transactions I
// # Write your MySQL query statement below
// SELECT
// DATE_FORMAT(trans_date, '%Y-%m') AS month,
// country,
// COUNT(1) AS trans_count,
// SUM(state = 'approved') AS approved_count,
// SUM(amount) AS trans_total_amount,
// SUM(IF(state = 'approved', amount, 0)) AS approved_total_amount
// FROM Transactions
// GROUP BY 1, 2;
// "#
// }
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.