FastPrepCSV Row Serializer

CSV Row Serializer

Mercor logoMercor● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Serialize a matrix of textual cell values into CSV rows.

Join cells with commas and do not append a trailing comma. A cell containing a comma, double quote, carriage return, or newline must be enclosed in double quotes. Inside a quoted cell, represent each literal double quote as two double quotes. Return one serialized string per input row.

Function

serializeCsvRows(rows: String[][]) → String[]

Examples

Example 1

rows = [["name","age"],["Ada","37"]]return = ["name,age","Ada,37"]

Plain cells need no quoting and rows have no trailing comma.

Example 2

rows = [["a,b","He said \"hi\"","line1\nline2"]]return = ["\"a,b\",\"He said \"\"hi\"\"\",\"line1\nline2\""]

Special cells are quoted and internal quotes are doubled.

Example 3

rows = [[],["","x"]]return = ["",",x"]

An empty row becomes an empty line; an empty first cell is represented before the comma.

Constraints

  • 0 <= rows.length <= 100000.
  • The total number of cells is at most 500000.
  • The total input text length is at most 2000000 characters.
  • Every cell is a non-null string. Callers convert non-string source values to their textual representation before invoking this portable adapter.

More Mercor problems

See Mercor hiring insights
public String[] serializeCsvRows(String[][] rows) {
    // Write your solution here.
}
rows[["name","age"],["Ada","37"]]
expected["name,age", "Ada,37"]
Checking account…