Inner join of two CSV datasets
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Stripe's HackerRank assessment, last asked Apr 2026. It tests hash map, parsing and joins.
Two systems export CSV data that you need to combine. Implement:
def join_datasets(customer_file: list[str], processor_file: list[str], field: str) -> list[str]
Each dataset is a list of lines. The first line is a header of comma-separated column names. Every other line is a data row with one value per column. Both headers contain the column field, but it can be at a different position in each file. Values never contain commas or quotes.
Return an inner join on field, as a list of CSV lines:
- The first line is the customer header followed by the processor header.
- Then, for each customer row (in input order), add one line for every processor row whose
fieldvalue is equal (in processor order). Each line holds all customer values followed by all processor values. - Customer rows with no match, and processor rows with no match, do not appear.
- A key can repeat in either file. Every matching pair produces a line.
Constraints: up to 10^5 rows per file and up to 20 columns. Comparisons are exact string matches.
Example
customer_file = ["cust_id,name,country", "c1,Ada,US", "c2,Lin,SG", "c3,Ola,NG"]
processor_file = ["amount,cust_id", "500,c1", "120,c3", "75,c1", "900,c9"]
field = "cust_id"
Output:
["cust_id,name,country,amount,cust_id",
"c1,Ada,US,500,c1",
"c1,Ada,US,75,c1",
"c3,Ola,NG,120,c3"]
c2 has no charge and c9 has no customer, so neither appears. c1 has two charges, so it appears twice.
Later parts may ask for a left join that keeps unmatched customer rows, and for options that change which rows are kept.
Clarifying questions
- Can a header list
fieldmore than once, or have duplicate column names in general? - Should a data row with the wrong number of values raise an error or be skipped?
- Is a header-only file valid input, and should the output then be only the joined header?
- Should values be trimmed, or is
" c1"different from"c1"? - Do both files fit in memory?
What a strong answer covers
- Finds the position of
fieldin each header separately. - Builds a hash map from key to the list of processor rows, in input order. Then it scans the customer rows in order and writes one line for each matching processor row. That is O(C + P + output) time, and the output order follows the rules without any sorting.
- Points out that a repeated key on both sides gives
count_c x count_plines, so the output can be much larger than the input. - Keeps the key column twice in the output, as the rule says, and does not drop or rename it.
- Tests repeated keys on both sides, no matches, the key in a different column in each file, and empty files.
Follow-ups
- A left join that keeps customer rows with no match, and what to write for the missing processor values.
- Files too large for memory: sort both by key and merge, or partition both by a hash of the key.
- Real CSV, with quoted values that contain commas and newlines.
- Join on several fields at once.
Where it's asked
- Stripe · HackerRank assessment · Last asked Apr 2026Leveled HackerRank task; test cases map to the parts in order
What it tests
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
