Build a Profile Sentence from CSV
Reported by candidates from Airbnb's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Airbnb reportedly put this one in front of candidates in November 2019, and the catch is right in the statement: a quoted interests field can hold commas and doubled quotes. Your first instinct is row.split(","), and that's exactly what breaks on Example 2 and Example 3. It's a string parsing problem dressed up as a sentence builder. If you've got an OA coming, know that the hard part is a tiny state machine, not the formatting. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but this one is easy to own before you start.
The problem
A profile is supplied as one valid CSV record with exactly seven fields in this order: firstname, lastname, email, interests, notes, city, and age. An unquoted comma separates fields. A field enclosed in double quotes may contain commas. Inside a quoted field, two consecutive double quotes decode to one literal double quote. Parse the record and return exactly: <firstname> is <age> years old, lives in <city>, and is interested in <interests>. Use the decoded values without adding or removing characters. Function profileSentence(row: String) → String Examples Example 1 row = "Ada,Lovelace,ada@example.com,mathematics,first programmer,London,36" return = "Ada is 36 years old, lives in London, and is interested in mathematics." No field is quoted, so the six commas separate the seven fields directly. Example 2 row = "Ada,Lovelace,ada@example.com,\"math, poetry\",first programmer,London,36" return = "Ada is 36 years old, lives in London, and is interested in math, poetry." The comma inside the quoted interests field is data rather than a delimiter. Example 3 row = "Grace,Hopper,grace@example.com,\"compilers and \"\"debugging\"\"\",navy pioneer,Arlington,85" return = "Grace is 85 years old, lives in Arlington, and is interested in compilers and \"debugging\"." Each doubled quote inside the quoted interests field decodes to one literal quote. Constraints 7 <= row.length <= 10^5. row is one valid single-line CSV record with exactly seven non-empty fields. No whitespace appears outside a quoted field. Double quotes appear only as the delimiters of a quoted field or as doubled quote escapes inside one. The decoded firstname, interests, city, and age fields are non-empty.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is a single left-to-right scan with an inQuotes flag. Keep a buffer for the current field. On a quote character while inside quotes, peek at the next character: if it's another quote, append one literal quote and skip ahead. Otherwise the quoted field is closing, so flip the flag. On a quote outside quotes, flip the flag on. On a comma outside quotes, push the buffer to the fields list and reset it. At the end, push the last buffer. Then fields[0], fields[5], fields[6] and fields[3] feed the template. The common pitfall is toggling the flag on every quote, which mangles escaped quotes, or using split and regex hacks. Length reaches 10^5, so build with a list or StringBuilder, not repeated concatenation. If the lookahead logic slips under pressure, StealthCoder is the hedge during the live OA.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Build a Profile Sentence from CSV cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Airbnb's OA.
Airbnb reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Build a Profile Sentence from CSV FAQ
What's the trick in the Airbnb profile sentence problem?+
Don't split on commas. Scan character by character with an inQuotes flag. Commas only end a field when you're outside quotes, and a pair of quotes inside a quoted field becomes one literal quote. Once you have seven fields, the sentence is a simple template.
How hard is this problem really?+
Easy to medium. There's no fancy data structure, just careful string handling. Most failures come from the escaped quote case in Example 3, where candidates toggle the quote flag twice and drop the quote characters from the output.
Can I use a regex or a CSV library?+
Risky. A regex for quoted fields with doubled quotes gets ugly fast and can be slow on 10^5 characters. A library may not be available in the assessment. A hand-written scanner is about fifteen lines and fully under your control.
Which fields do I actually need from the row?+
Index 0 for firstname, 3 for interests, 5 for city, and 6 for age. Lastname, email, and notes only need to be parsed correctly so the indexes line up. The output is the decoded values with no added or removed characters.
How do I prepare for this in 48 hours?+
Write the quote-aware scanner from scratch twice and test it on the three examples. Add your own cases: a quoted field at the end, a field that is only an escaped quote, and a long row. Then confirm you use a StringBuilder or list for the buffer.