Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Clean Dataset Records

Easy
EasyCodingHash TablesArraysStrings

Problem

A data team at Acme Analytics stores raw dataset rows as Python dictionaries. Write a function to clean and preprocess these records by normalizing text fields, replacing missing values, and removing duplicate rows.

Task

Given a list of records, where each record is a dictionary with string keys and values that may be strings, numbers, or None, return a new list of cleaned records.

A record should be cleaned using these rules:

  1. Trim leading and trailing spaces from all string values.
  2. Convert empty strings (after trimming) to "UNKNOWN".
  3. Replace None values with "UNKNOWN".
  4. Convert all string values to lowercase.
  5. Remove duplicate records after cleaning. Two records are duplicates if they contain the same key-value pairs.
  6. Preserve the order of the first occurrence of each unique cleaned record.

Constraints

  • 1 <= len(records) <= 10^4
  • Each record contains between 1 and 20 keys
  • Keys are non-empty strings
  • String values have length at most 200

Function Signature

def clean_dataset(records):
Take this as a live interview session →

You are practicing as a guest. Sign up free to run your code against the sample data. Your draft stays right here.

Sign up freeI have an account
def solve(rows):
    counts = {}
    for row in rows:
        ...
    return result
Sign up to unlock solutions
Next questions
Cleaning Data in PythonMediumNCRPython Data Cleaning ProcessMediumClean and Summarize Event RecordsEasy