← Problems25. Parse fixed-width log linesEasyPython
00:00 / 20:00

Parse fixed-width log lines

Easy·Acceptance ·Asked at Walmart, Deloitte

A legacy system writes fixed-width records. Parse them into dictionaries, trimming padding and skipping malformed lines.

Input schema

solve(lines: list[str], layout: list[tuple[str, int, int]]) -> list[dict] layout entries are (field_name, start_index, length)

Example

layout = [("id", 0, 4), ("city", 4, 8)] lines = ["0012Mumbai ", "0013Pune ", "bad"] Output = [{"id": "0012", "city": "Mumbai"}, {"id": "0013", "city": "Pune"}]

Constraints

  • A line shorter than the layout requires is malformed and skipped
  • Trailing and leading spaces are stripped from every field
  • Field order in the output follows the layout
  • Up to 10,000,000 lines

Topics

data modeling

Similar problems

Community-reported interview topic. Not an official company question and no affiliation is implied.

solution.py
Loading editor…
Draft not saved yet · Spaces 4 · UTF-8 · ⌘↵ run, ⌘⇧↵ submit
Nothing run yet

Run against the public tests, or submit to score against all of them.