Skip to main content
Back to problems
#1613
Medium Database

Find the missing ids

Database
73.0% acceptance
Mar 31, 2026
238
31

No description available.

Solution

Pandas
Time O(n)
Space O(1)
LeetCode
solution.pandas
# 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.
# 
#  
# 
# Write a solution to find the missing customer IDs. The missing IDs are ones that are not in the Customers table but are in the range between 1 and the maximum customer_id present in the table.
# 
# Notice that the maximum customer_id will not exceed 100.
# 
# Return the result table ordered by ids in ascending order.
# 
# The result format is in the following example.
#
# Example 1:
# Input:
# Customers table:
# +-------------+---------------+
# | customer_id | customer_name |
# +-------------+---------------+
# | 1           | Alice         |
# | 4           | Bob           |
# | 5           | Charlie       |
# +-------------+---------------+
# Output:
# +-----+
# | ids |
# +-----+
# | 2   |
# | 3   |
# +-----+
# Explanation:
# The maximum customer_id present in the table is 5, so in the range [1,5], IDs 2 and 3 are missing from the table.

import pandas as pd

def find_missing_ids(customers: pd.DataFrame) -> pd.DataFrame:
  max_id = customers['customer_id'].max()
  all_ids = set(range(1, max_id + 1))
  existing = set(customers['customer_id'])
  missing = sorted(all_ids - existing)
  return pd.DataFrame({'ids': missing})