Katkat

Refactor code and enforce robustness

Capital OneData Scientist · Onsite

Last updated

This question evaluates proficiency in code refactoring, robustness, input validation, unit testing, environment specification, and complexity analysis for Python data-processing workflows (pandas), within the Coding & Algorithms domain for Data Scientist roles.

You are given this Python script:

script.py

import pandas as pd DATA_PATH = 'data.csv' result = None

def compute_total(col): df = pd.read_csv(DATA_PATH) total = 0 for x in df[col]: if x == '': total += 0 else: total += float(x) print(total)

compute_total('amount')

Tasks: (1) Identify at least five defects or risks (correctness, performance, readability, resource management, security). (2) Refactor into a small, testable module with clear interfaces, type hints, and no global state; include input validation and assert-based precondition checks (and explain when assertions vs. exceptions are appropriate). (3) Write three pytest-style unit tests using assert statements that cover normal, missing/NaN, and malformed inputs. (4) Provide an environment.yml for a Conda environment (Python 3.11, pinned dependencies) and the exact commands to create/activate it. (5) Explain the benefits of modularization for maintainability, dependency management, and testability. (6) State the time/space complexity before and after refactoring and any I/O bottlenecks you’d address.