FastPrepParse a CSV Record

Parse a CSV Record

ByteDance logoByteDance● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

The interview report describes implementing a data parser. For this exercise, assume the input line is one valid CSV record, and return its decoded fields in order.

  • An unquoted comma separates fields.
  • A field may be enclosed in double quotes. A quoted field can contain commas; inside it, two consecutive double quotes encode one literal double quote.
  • An unquoted field contains no comma or double quote. A quoted field closes immediately before a separator or the end of the record.
  • Preserve spaces and all other data characters exactly. Backslashes have no special meaning.
  • Preserve empty fields, including leading, consecutive, and trailing empty fields. An empty record contains one empty field.

The record contains only printable ASCII characters. It is guaranteed to follow this grammar; multiline input, header interpretation, type conversion, and malformed-input recovery are outside scope.

Function

parseCsvRecord(line: String) → String[]

Examples

Example 1

line = "alpha,\"beta,gamma\",\"said \"\"yes\"\"\""return = ["alpha","beta,gamma","said \"yes\""]

The comma inside the second quoted field is data. The doubled quotes in the third field each decode to a literal double quote.

Example 2

line = ",left,,right,"return = ["","left","","right",""]

Keep the empty field before the first comma, between consecutive commas, and after the final comma.

Example 3

line = "\" spaced \",plain"return = [" spaced ","plain"]

Remove the enclosing quotes while preserving both spaces inside the first field.

Constraints

  • 0 <= line.length <= 10000.
  • Each character is printable ASCII, with code point from 32 through 126.
  • The record follows the stated valid CSV grammar.

More ByteDance problems

See ByteDance hiring insights
public String[] parseCsvRecord(String line) {
    // Write your code here.
}
line"alpha,\"beta,gamma\",\"said \"\"yes\"\"\""
expected["alpha", "beta,gamma", "said \"yes\""]
Checking account…