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: Signups
+----------------+----------+ | Column Name | Type | +----------------+----------+ | user_id | int | | time_stamp | datetime | +----------------+----------+ user_id is the column of unique values for this table. Each row contains information about the signup time for the user with ID user_id.
Table: Confirmations
+----------------+----------+
| Column Name | Type |
+----------------+----------+
| user_id | int |
| time_stamp | datetime |
| action | ENUM |
+----------------+----------+
(user_id, time_stamp) is the primary key (combination of columns with unique values) for this table.
user_id is a foreign key (reference column) to the Signups table.
action is an ENUM (category) of the type ('confirmed', 'timeout')
Each row of this table indicates that the user with ID user_id requested a confirmation message at time_stamp and that confirmation message was either confirmed ('confirmed') or expired without confirming ('timeout').
The confirmation rate of a user is the number of 'confirmed' messages divided by the total number of requested confirmation messages. The confirmation rate of a user that did not request any confirmation messages is 0. Round the confirmation rate to two decimal places.
Write a solution to find the confirmation rate of each user.
Return the result table in any order.
The result format is in the following example.
Example 1:
Input: Signups table: +---------+---------------------+ | user_id | time_stamp | +---------+---------------------+ | 3 | 2020-03-21 10:16:13 | | 7 | 2020-01-04 13:57:59 | | 2 | 2020-07-29 23:09:44 | | 6 | 2020-12-09 10:39:37 | +---------+---------------------+ Confirmations table: +---------+---------------------+-----------+ | user_id | time_stamp | action | +---------+---------------------+-----------+ | 3 | 2021-01-06 03:30:46 | timeout | | 3 | 2021-07-14 14:00:00 | timeout | | 7 | 2021-06-12 11:57:29 | confirmed | | 7 | 2021-06-13 12:58:28 | confirmed | | 7 | 2021-06-14 13:59:27 | confirmed | | 2 | 2021-01-22 00:00:00 | confirmed | | 2 | 2021-02-28 23:59:59 | timeout | +---------+---------------------+-----------+ Output: +---------+-------------------+ | user_id | confirmation_rate | +---------+-------------------+ | 6 | 0.00 | | 3 | 0.00 | | 7 | 1.00 | | 2 | 0.50 | +---------+-------------------+ Explanation: User 6 did not request any confirmation messages. The confirmation rate is 0. User 3 made 2 requests and both timed out. The confirmation rate is 0. User 7 made 3 requests and all were confirmed. The confirmation rate is 1. User 2 made 2 requests where one was confirmed and the other timed out. The confirmation rate is 1 / 2 = 0.5.
Problem summary: Table: Signups +----------------+----------+ | Column Name | Type | +----------------+----------+ | user_id | int | | time_stamp | datetime | +----------------+----------+ user_id is the column of unique values for this table. Each row contains information about the signup time for the user with ID user_id. Table: Confirmations +----------------+----------+ | Column Name | Type | +----------------+----------+ | user_id | int | | time_stamp | datetime | | action | ENUM | +----------------+----------+ (user_id, time_stamp) is the primary key (combination of columns with unique values) for this table. user_id is a foreign key (reference column) to the Signups table. action is an ENUM (category) of the type ('confirmed', 'timeout') Each row of this table indicates that the user with ID user_id requested a confirmation message at time_stamp and that confirmation message was either confirmed
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
{"headers": {"Signups": ["user_id", "time_stamp"], "Confirmations": ["user_id", "time_stamp", "action"]}, "rows": {"Signups": [[3, "2020-03-21 10:16:13"], [7, "2020-01-04 13:57:59"], [2, "2020-07-29 23:09:44"], [6, "2020-12-09 10:39:37"]], "Confirmations": [[3, "2021-01-06 03:30:46", "timeout"], [3, "2021-07-14 14:00:00", "timeout"], [7, "2021-06-12 11:57:29", "confirmed"], [7, "2021-06-13 12:58:28", "confirmed"], [7, "2021-06-14 13:59:27", "confirmed"], [2, "2021-01-22 00:00:00", "confirmed"], [2, "2021-02-28 23:59:59", "timeout"]]}}Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1934: Confirmation Rate
// Auto-generated Java example from rust.
class Solution {
public void exampleSolution() {
}
}
// Reference (rust):
// // Accepted solution for LeetCode #1934: Confirmation Rate
// pub fn sql_example() -> &'static str {
// r#"
// -- Accepted solution for LeetCode #1934: Confirmation Rate
// # Write your MySQL query statement below
// SELECT
// user_id,
// ROUND(IFNULL(SUM(action = 'confirmed') / COUNT(1), 0), 2) AS confirmation_rate
// FROM
// SignUps
// LEFT JOIN Confirmations USING (user_id)
// GROUP BY 1;
// "#
// }
// Accepted solution for LeetCode #1934: Confirmation Rate
// Auto-generated Go example from rust.
func exampleSolution() {
}
// Reference (rust):
// // Accepted solution for LeetCode #1934: Confirmation Rate
// pub fn sql_example() -> &'static str {
// r#"
// -- Accepted solution for LeetCode #1934: Confirmation Rate
// # Write your MySQL query statement below
// SELECT
// user_id,
// ROUND(IFNULL(SUM(action = 'confirmed') / COUNT(1), 0), 2) AS confirmation_rate
// FROM
// SignUps
// LEFT JOIN Confirmations USING (user_id)
// GROUP BY 1;
// "#
// }
# Accepted solution for LeetCode #1934: Confirmation Rate
# Auto-generated Python example from rust.
def example_solution() -> None:
return
# Reference (rust):
# // Accepted solution for LeetCode #1934: Confirmation Rate
# pub fn sql_example() -> &'static str {
# r#"
# -- Accepted solution for LeetCode #1934: Confirmation Rate
# # Write your MySQL query statement below
# SELECT
# user_id,
# ROUND(IFNULL(SUM(action = 'confirmed') / COUNT(1), 0), 2) AS confirmation_rate
# FROM
# SignUps
# LEFT JOIN Confirmations USING (user_id)
# GROUP BY 1;
# "#
# }
// Accepted solution for LeetCode #1934: Confirmation Rate
pub fn sql_example() -> &'static str {
r#"
-- Accepted solution for LeetCode #1934: Confirmation Rate
# Write your MySQL query statement below
SELECT
user_id,
ROUND(IFNULL(SUM(action = 'confirmed') / COUNT(1), 0), 2) AS confirmation_rate
FROM
SignUps
LEFT JOIN Confirmations USING (user_id)
GROUP BY 1;
"#
}
// Accepted solution for LeetCode #1934: Confirmation Rate
// Auto-generated TypeScript example from rust.
function exampleSolution(): void {
}
// Reference (rust):
// // Accepted solution for LeetCode #1934: Confirmation Rate
// pub fn sql_example() -> &'static str {
// r#"
// -- Accepted solution for LeetCode #1934: Confirmation Rate
// # Write your MySQL query statement below
// SELECT
// user_id,
// ROUND(IFNULL(SUM(action = 'confirmed') / COUNT(1), 0), 2) AS confirmation_rate
// FROM
// SignUps
// LEFT JOIN Confirmations USING (user_id)
// 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.