Compute costs with validation and sorting in Python
Company: Stripe
Role: Software Engineer
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Technical Screen
Implement a three-part Python task to compute costs for purchase line items.
Part 1: Write compute_cost(line_items, price_db) where line_items is a list of dicts like {"product_id": str, "qty": int} and price_db is a dict mapping product_id -> unit_price (float). Return (total_cost, breakdown) where breakdown lists per-item cost. Clarify and implement behavior when a product_id is missing from price_db (e.g., raise, skip with warning, or default).
Part 2: Add robust validation: quantities/prices must be numeric; qty must be nonnegative; reject NaN/inf; detect and report invalid rows with clear errors. Include tests for empty input, large values, and duplicate product_ids (define whether to sum or treat as separate lines).
Part 3: If sort=True, return breakdown sorted by per-item cost descending using a lambda key; otherwise preserve input order. Ensure outputs match expected results and document rounding rules.
Quick Answer: This question evaluates proficiency in data validation, numeric robustness, error handling, aggregation, per-item cost computation and deterministic sorting within Python-based data manipulation tasks, and it belongs to the Data Manipulation (SQL/Python) domain.