Parse a delimited inventory line
Python data engineering interview problem. Difficulty: beginner. Pattern: Strings. About 10 minutes. Free to practice.
Inventory comes in as one delimited line from a handheld scanner. You need named fields, not a raw string, before it can land in a warehouse table.
Inventory arrives as one line of text per product, with the fields separated by a pipe |. The system needs a dictionary with the right types. Split the line into sku, name, qty and price. Keep sku and name as text, turn qty into an integer and price into a decimal number (float). Return a dictionary. Write parse_inventory_line(line). 'SKU-1|widget|12|3.5' becomes {'sku': 'SKU-1', 'name': 'widget', 'qty': 12, 'price': 3.5}. 'A|x|0|0' gives qty 0 and price 0.0. The starter file already has the function name and its print lines. Keep them and fill in the body.
Requirements
- Split the line on | into 4 fields.
- qty must be an int.
- price must be a float.
- Return a dictionary with the keys sku, name, qty, price.
Constraints
- Each line has exactly 4 fields.
- Numbers are written as plain digits, with an optional decimal point.
Examples
Input: parse_inventory_line('SKU-1|widget|12|3.5') Output: {'sku': 'SKU-1', 'name': 'widget', 'qty': 12, 'price': 3.5} This input follows the stated rules and produces this output.
Topics: lakebench, python, parsing.
More Python interview questions · All interview problems · Learn data engineering
Parse a delimited inventory line
Interview-style drill: Parse a pipe-delimited inventory record into a structured dictionary.
Parse a delimited inventory line into the structured fields the tests expect.