Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Parse and Clean Noisy CSV
00:00
5 left

Parse and Clean Noisy CSV

HardPython

Problem

Write a Python script to efficiently parse and clean a massive, noisy CSV file generated by an industrial sensor for Ametek.

Implement clean_sensor_csv(lines), where lines is an iterable of CSV text lines whose first nonblank line is the header timestamp,sensor_id,value,status. Return a list of dictionaries with stripped timestamp, stripped sensor_id, finite floating-point value, and uppercase status. Ignore blank lines, malformed CSV rows, rows with anything other than four fields, missing required fields, invalid numbers, and nonfinite values.

Example: ['timestamp,sensor_id,value,status ', '2026-01-01T00:00:00Z, S-17, 21.5, ok '] returns [{'timestamp': '2026-01-01T00:00:00Z', 'sensor_id': 'S-17', 'value': 21.5, 'status': 'OK'}]. Process the input in one pass using standard-library Python tools.

Constraints

  • lines is an iterable of strings.
  • The first nonblank line is the CSV header.
  • Each valid data row has exactly four fields in the order timestamp,sensor_id,value,status.
  • Input lines may be noisy, malformed, blank, or contain invalid numeric values.
  • The algorithm should process the input in one pass.
  • Use only Python standard-library modules.

Function Signature

def clean_sensor_csv(lines):
Interviewer

Your question is Parse and Clean Noisy CSV. Start with the requirements 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.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.