The Find Median Given Frequency of Numbers coding problem is a SQL task. You are given a table Numbers with columns number and frequency. You need to calculate the median of all the numbers in the dataset. Since the data is grouped by frequency, you are essentially dealing with a frequency distribution table.
Pinterest and other companies use this to test advanced SQL skills, specifically Cumulative Aggregation and Window Functions. Calculating a median in SQL is notoriously difficult because standard SQL doesn't have a built-in MEDIAN() function. It evaluates whether you can use cumulative sums to find the "middle" positions in a sorted frequency list.
This problem follows the Cumulative Frequency pattern.
running_sum of frequencies for each number when sorted.running_sum - frequency to running_sum) covers one of those middle positions.| Number | Freq | Cumulative |
|---|---|---|
| 0 | 7 | 7 |
| 1 | 1 | 8 |
| 2 | 3 | 11 |
| Total . Middle position is . |
If , middle positions are 6 and 7. Both are covered by Number 0. Median = (0+0)/2 = 0.
number column without considering how many times each number appears.Practice the SUM(freq) OVER(ORDER BY number) window function. It is the key to handling frequency-based data in SQL. Understanding how to find the "median row" using cumulative counts is a high-level skill that differentiates senior candidates.