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

Parse fixed-width log lines

Easy19% solve itAsked 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)

Constraints

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

Example 1two good lines and one malformed

Input · lines
["0012Mumbai  ", "0013Pune    ", "bad"]
Input · layout
[["id", 0, 4], ["city", 4, 8]]
Output
idcity
0012Mumbai
0013Pune

Example 2padding is stripped from both ends

Input · lines
["  a  bb   "]
Input · layout
[["first", 0, 5], ["second", 5, 5]]
Output
firstsecond
abb

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.