Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Batch Rows From CSV
00:00
5 left

Batch Rows From CSV

HardPython

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):
Interviewer

Your question is Batch Rows From CSV. Start with the requirements in the Question tab.

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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.