Find the Missing IDs — LeetCode 1613 Python Solution

MediumLeetCode PremiumDatabase
Problem
#1613
Reading time
5 min

Table schema

SQL
Table: Customers +---------------+---------+ | Column Name | Type | +---------------+---------+ | customer_id | int | | customer_name | varchar | +---------------+---------+ customer_id is the column with unique values for this table. Each row of this table contains the name and the id customer.

Example

SQL
+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| customer_id   | int     |
| customer_name | varchar |
+---------------+---------+
customer_id is the column with unique values for this table.
Each row of this table contains the name and the id customer.

Python solution

Python
import duckdb
import pandas as pd

def solution(customers: pd.DataFrame) -> pd.DataFrame:
    con = duckdb.connect()
    con.register("Customers", customers)
    return con.execute("""WITH RECURSIVE
    t AS (
        SELECT
            1 AS n
        UNION ALL
        SELECT
            n + 1
        FROM t
        WHERE n < 100
    )
SELECT
    n AS ids
FROM t
WHERE
    n < (
        SELECT
            MAX(customer_id)
        FROM Customers
    )
    AND n NOT IN (
        SELECT
            customer_id
        FROM Customers
    );""").df()

Complexity

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

Related problems

Frequently asked questions

How hard is LeetCode 1613. Find the Missing IDs?
LeetCode 1613. Find the Missing IDs is rated Medium on LeetCode.
What topics does LeetCode 1613. Find the Missing IDs cover?
LeetCode 1613. Find the Missing IDs is tagged Database on LeetCode.
Is LeetCode 1613. Find the Missing IDs a premium problem?
Yes. LeetCode 1613. Find the Missing IDs is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.

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