Parse a CSV Record
Updated
Problem statement
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
32through126. - The record follows the stated valid CSV grammar.
More ByteDance Phone Screen questions
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Implement QuicksortPHONE SCREEN · Seen Dec 2026
- Largest Rectangle in HistogramPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Reverse Linked ListPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Count N-Queens PlacementsPHONE SCREEN · Seen Nov 2026
- Matrix EscapePHONE SCREEN · Seen Nov 2026
- Largest Number Below N from Reusable DigitsPHONE SCREEN · Seen Nov 2026
- Log Analyzer: Busiest Service in a Rolling WindowPHONE SCREEN · Seen Nov 2026
- Majority ElementPHONE SCREEN · Seen Nov 2026
- Valid ParenthesesPHONE SCREEN · Seen Nov 2026
Similar questions at other companies
- Route Pattern MatcherGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Business Account KYC Verification — Parts 1–5Stripe · PHONE SCREENStripe interview questions
- Dasher Pay with Store Waits and Peak HoursDoorDash · PHONE SCREENDoorDash interview questions
- Minimum Number of Frogs CroakingCerebras · PHONE SCREEN
- Adjacent Pair and Run RemovalWhatNot · PHONE SCREEN · ONSITE INTERVIEWWhatNot interview questions
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Read Across Sound TracksAirbnb · PHONE SCREENAirbnb interview questions
- Stateful Log Transform and SearchRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- K-Step Bit Flip with RetentionCisco · PHONE SCREENCisco interview questions
ByteDance interview experiences
- ByteDance TikTok USDS SWE Intern Interview: 1 Experience
- ByteDance MLE New Grad Interviews: 1 Experience
- ByteDance Security SRE Intern Phone Screens: 1 Experience
- ByteDance USDS New Grad SWE Phone Screen: 1 Experience
- ByteDance New Grad Data Engineer Video Interviews: 1 Experience
- ByteDance Trust & Safety Research Scientist Intern: 1 Experience