Median Search Frequency

ASKED IN INTERVIEW
10pts
Google

Problem Statement

Google data analysts often work with compressed datasets where logs are pre-aggregated into frequency distributions to save storage space. Instead of having one row per user search, the Search_Stats table tells you how many users performed a specific number of searches.

Your objective is to find the median number of searches per user.

Understanding the Data:

If the table shows search_count 1 with user_count 3, and search_count 2 with user_count 2, the underlying distribution is [1, 1, 1, 2, 2]. The median here is 1.

Rules:

  • If the total number of users is odd, the median is the middle value.
  • If the total number of users is even, the median is the average of the two middle values.
  • Return the result as a single column named median.
  • Round the result to 1 decimal place.

Table Schema:

  • Search_Stats: search_count (number of searches performed), user_count (number of users who performed that many searches).
  • Search_Metadata: category_id, category_name (context for search types).
  • Search_Logs: log_id, user_id, search_date (raw log details).
Tests your understanding of
Window Functions, Median, Mathematical Logic, CTE and Running Totals

Input Tables

Search_Stats
search_count(INTEGER)user_count(INTEGER)
115
28
312
410
55
102
151
Search_Metadata
category_id(INTEGER)category_name(VARCHAR)
1Web
2Images
3News
4Maps
5Shopping
6Video
Search_Logs
log_id(INTEGER)user_id(INTEGER)search_date(DATE)
15012026-02-01
25022026-02-01
35032026-02-02
45042026-02-02
55052026-02-03
65062026-02-03

Expected Output

median(DECIMAL)
3

Tags

HardASKED IN INTERVIEWWindow FunctionsMedianMathematical LogicCTERunning Totals
25-30 min
31%

Hints