Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Get started
Category and Dataset Median Imputation
00:00
5 left

Category and Dataset Median Imputation

MediumSQL · PostgreSQL

Problem

How do you handle missing data in a dataset?

Using the provided observation, category, and source data, produce a cleaned result. Treat scores outside the range 0 through 100 as missing, retain every observation, and apply category-level then dataset-level fallbacks for missing scores. Replace missing or blank text values with the appropriate category or generic fallback.

Output

  1. One row per observation, ordered by entity_id, observed_on, and observation_id
  2. Return observation_id, entity_id, observed_on, category_name, cleaned_score, score_was_missing, cleaned_status, source_name, and entity_sequence
  3. Use Unclassified, Unknown, or 0 only when no more specific fallback exists

Output details

  • Order entity_id, observed_on, and observation_id ascending, using observation_id to break ties.
  • Compute category and dataset medians using continuous percentile interpolation, excluding missing scores and preserving fractional values without rounding.
  • Mark score_was_missing true for NULL or out-of-range scores, and false for scores from 0 through 100 inclusive.
  • For cleaned_score, use the category median, then dataset median, then 0 when no valid score exists.
  • Trim status whitespace before treating it as blank, then use the category default status, or Unknown.
  • Use Unclassified for missing category names and Unknown for missing source names; number entity_sequence from 1 per entity in ascending observation order.

Schema

observations
ColumnTypeDescription
observation_idPKINTUnique observation identifier
entity_idVARCHAR(20)Entity associated with the observation
category_idINTOptional category reference
observed_onDATEDate of the observation
scoreNUMERIC(6,2)Observed score, expected between 0 and 100
statusVARCHAR(30)Observed status
source_idINTOptional data source reference
categories
ColumnTypeDescription
category_idPKINTUnique category identifier
category_nameVARCHAR(40)Category label
default_statusVARCHAR(30)Fallback status for the category
data_sources
ColumnTypeDescription
source_idPKINTUnique source identifier
source_nameVARCHAR(40)Source label
source_typeVARCHAR(30)Source classification
Tablesobservationscategoriesdata_sources
Interviewer

Your question is Category and Dataset Median Imputation. Start with the requirements and the three tables in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePostgreSQL
Sign up free to run your codeLog inLn 1
Run your query to see results here.