KatInterview

Find Highest Weighted Venue Locations with Data Cleaning

SQL & DataModel answer
MediumModel answerSQL & DataSQLPythonData Cleaning

Last updated

Compute top venue locations by summing vote weights using SQL and Python, while handling missing values, text normalization, and ties.

You are given a dataset containing user submissions for preferred event venues. Each entry consists of a voter_id, a string representing the location_text, and an optional numeric weight (defaults to 1 when unsupplied).

Write a solution in SQL and/or Python to determine the location(s) with the highest total vote weight.

Your solution must address the following requirements:

  1. Normalization: Treat inputs like " New York ", "new york", and "NEW YORK" as identical. Standardize location text by stripping leading/trailing spaces, collapsing consecutive internal whitespace, and normalizing letter case.
  2. Missing or Invalid Locations: Ignore records where location_text is NULL, missing, or reduces to an empty string after trimming.
  3. Weight Parsing:
    • A missing or NULL weight defaults to 1.
    • Non-numeric strings (e.g., "abc"), boolean values, non-finite values (NaN, Infinity), or negative weights must be considered invalid and discarded.
  4. Tie Handling: If multiple locations share the max aggregate weight, return all tied locations sorted alphabetically.
  5. Deduplication: Assume each row represents an independent valid vote unless stated otherwise (treat rows as additive).

Provide both SQL and Python implementations, walk through a sample execution, and analyze the time and space complexity.