Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Text Parsing and Aggregation
00:00
5 left

Text Parsing and Aggregation

MediumPython

Problem

Practical HackerRank exercise: parse text input and aggregate it (e.g., aggregating transactions from a CSV).

Asked in the tech screen stage.

Implement parse_and_aggregate(csv_text). The input is a CSV string with a header category,amount and one transaction per subsequent row. Return a dictionary mapping each category to the sum of its amounts. Categories may contain quoted commas, and repeated categories must be combined. Ignore empty lines. Amounts are integers.

Constraints

  • The header is present and exactly category,amount.
  • There are at most 1,000 data rows.
  • Each amount fits in a 32-bit signed integer.
  • Each non-empty data row contains exactly two CSV fields.

Function Signature

def parse_and_aggregate(csv_text):
Interviewer

Your question is Text Parsing and Aggregation. 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
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.