#571
Hard Database Find median given frequency of numbers
Database
42.2% acceptance
Mar 31, 2026
319
79
No description available.
Solution
Pandas
Time O(n)
Space O(1)
# Table: Numbers
#
# +-------------+------+
# | Column Name | Type |
# +-------------+------+
# | num | int |
# | frequency | int |
# +-------------+------+
# num is the primary key (column with unique values) for this table.
# Each row of this table shows the frequency of a number in the database.
#
#
#
# The median is the value separating the higher half from the lower half of a data sample.
#
# Write a solution to report the median of all the numbers in the database after decompressing the Numbers table. Round the median to one decimal point.
#
# The result format is in the following example.
#
# Example 1:
# Input:
# Numbers table:
# +-----+-----------+
# | num | frequency |
# +-----+-----------+
# | 0 | 7 |
# | 1 | 1 |
# | 2 | 3 |
# | 3 | 1 |
# +-----+-----------+
# Output:
# +--------+
# | median |
# +--------+
# | 0.0 |
# +--------+
# Explanation:
# If we decompress the Numbers table, we will get [0, 0, 0, 0, 0, 0, 0, 1, 2, 2, 2, 3], so the median is (0 + 0) / 2 = 0.
import pandas as pd
def median_frequency(numbers: pd.DataFrame) -> pd.DataFrame:
numbers = numbers.sort_values("num")
numbers["cum_freq"] = numbers["frequency"].cumsum()
total = numbers["frequency"].sum()
lower_pos = (total + 1) // 2
upper_pos = (total + 2) // 2
candidates = numbers[
(numbers["cum_freq"] >= lower_pos)
& (numbers["cum_freq"] - numbers["frequency"] < upper_pos)
]
return pd.DataFrame({"median": [round(candidates["num"].mean(), 1)]})