Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Batch Rows From CSV

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Batch Rows From CSV. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Adverity data ingestion pipelines process CSV content that may contain headers, quoted commas, escaped quotes, and fields spanning multiple physical lines. Implement yield_csv_batches to parse the input and return rows grouped into batches without manually splitting on commas.

Formal Specification

The function receives lines, an iterable of CSV text lines, batch_size, a positive integer, and has_header, a boolean. Return a list of batches. Each batch contains at most batch_size rows. If has_header is true, represent each data row as a dictionary mapping header names to string values. Otherwise, represent each row as a list of strings. Preserve the order of rows and fields, skip completely blank CSV records, and do not return the header as data.

Raise ValueError when batch_size is not positive or when a header contains duplicate names. An empty input, or an input containing only a header, returns an empty list.

Constraints

  • lines contains at most 10^6 physical lines
  • Each CSV record contains at most 10^3 fields
  • batch_size is expected to be a positive integer
  • CSV values are strings using standard CSV quoting rules
  • The complete returned list is assumed to fit in memory

Function Signature

def yield_csv_batches(lines, batch_size, has_header):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output