Confirmation Rate — LeetCode 1934 Python Solution

MediumDatabase
Problem
#1934
Reading time
3 min

Table schema

SQL
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.

Example

SQL
+----------------+----------+
| 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.

Python solution

Python
import duckdb
import pandas as pd

def solution(signups: pd.DataFrame, confirmations: pd.DataFrame) -> pd.DataFrame:
    con = duckdb.connect()
    con.register("Signups", signups)
    con.register("Confirmations", confirmations)
    return con.execute("""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;""").df()

Complexity

MeasureComplexity
TimeO(n log n) (typical)
SpaceO(n) auxiliary

Related problems

Frequently asked questions

How hard is LeetCode 1934. Confirmation Rate?
LeetCode 1934. Confirmation Rate is rated Medium on LeetCode.
What topics does LeetCode 1934. Confirmation Rate cover?
LeetCode 1934. Confirmation Rate is tagged Database on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview