KatInterview

Build a Holiday Gift Matcher from CSV Input

StringsModel answer
MediumModel answerStringsparsingvalidationalgorithms

Last updated

Parse participant data from a CSV string with quoted fields, validate entries, and create a round-robin assignment where no one gives a gift to themselves.

You're building a holiday gift exchange coordinator. Given participant information in CSV format, you need to parse the data, validate it, and generate assignments where each person gives exactly one gift and receives exactly one gift, with no one assigned to themselves.

Input Format

A CSV-formatted string where each line contains:

  • Participant name (may contain commas if quoted)
  • Email address

Example:

Alice,[email protected]
"Lee, Jordan",[email protected]
Taylor,[email protected]

Requirements

Parsing:

  • Handle quoted fields correctly (commas inside quotes should not split fields)
  • Support escaped quotes within quoted fields ("" represents a literal ")
  • Ignore empty lines
  • Handle both Unix (\n) and Windows (\r\n) line endings

Validation:

  • Each line must have exactly 2 fields (name and email)
  • Email addresses must be unique (case-insensitive)
  • Names and emails must be non-empty after trimming whitespace
  • Emails must contain an @ symbol
  • Raise descriptive errors with line numbers for invalid input
  • Require at least 2 participants

Assignment:

  • Create a mapping from each email to another email (the gift recipient)
  • No participant should be assigned to themselves
  • Accept an optional seed parameter for deterministic output

Output

Return a dictionary mapping each participant's email to their assigned recipient's email.

Example output:

{
  "[email protected]": "[email protected]",
  "[email protected]": "[email protected]",
  "[email protected]": "[email protected]"
}

Constraints

  • 2 ≤ number of participants ≤ 10,000
  • Emails are the unique identifier for each participant

Hints

Hint 1

Build a character-by-character parser that tracks whether you're inside quotes to handle embedded commas correctly

Hint 2

Normalize email addresses by converting to lowercase and trimming whitespace before checking for duplicates

Hint 3

Once you have a valid list of participants, shuffle them and create a circular assignment pattern to guarantee no self-assignments