Problem 276567 · medium · Phase 02 Linear Data Structures

Split a Quoted CSV Line

strings · parsing · state machines

Write split_csv(line) that splits one line of comma-separated values into a list of fields. A field may be wrapped in double quotes; inside quotes a comma is part of the field, and two consecutive quotes "" stand for one literal quote character. The wrapping quotes themselves are not part of the field.

An empty line yields a single empty field: [""].

Examples

Input:  line = 'name,"Smith, John",42'
Output: ["name", "Smith, John", "42"]

Input:  line = 'a,"b ""quoted"" c",'
Output: ["a", 'b "quoted" c', ""]
Explanation: the trailing comma produces an empty last field.

Input:  line = ""
Output: [""]

Constraints

  • 0 <= len(line) <= 10**5, printable ASCII
  • Quotes are balanced; a quoted field starts with "
  • Target: O(n) time

Goals

  • Track whether the scanner is inside quotes
  • Handle an escaped quote by looking one character ahead
  • Flush the final field after the loop
Starting Python…