← 返回 stripe 的题目列表Join Two Datasets by a Field (Inner/Left Join with One-to-Many and Optional Skipping)
类型:online_judge
Problem: Join Two Datasets by a Field (Inner/Left Join with One-to-Many and Optional Skipping)
Implement joinDataSet(fieldName, customerFile, processorFile, skipUnmatched) to join two “files”.
customerFile, processorFile are arrays of strings (string[]).
Line 1 is the header (column names).
Line 2+ are data rows.
fieldName is the join key column name (present in both headers).
skipUnmatched controls how to handle customers with no match.
Parsing
Use each header to locate the column index of fieldName in both files.
Process data rows in their original order.
Output
Return a new string[] representing the joined file:
First line is the output header:
Customer Header + Processor Header
i.e., all customer columns first, then all processor columns.
Subsequent lines are joined rows.
Join semantics
Treat customerFile as the left table and processorFile as the right table; join on equality of fieldName.
Matches (one-to-many):
If a customer row matches one or more processor rows, output one row per match:
customerRow + processorRow
No match (left join behavior):
If a customer row has no matching processor row:
If skipUnmatched == false, output exactly one row:
customerRow + (all processor columns empty)
If skipUnmatched == true, output nothing for that customer.
Ordering requirements (stability)
Output is primarily ordered by the customer rows’ original order in customerFile.
For a given customer with multiple processor matches, output those rows in the processor rows’ original order in processorFile.
Example
Input
fieldName = "customer_id"
skipUnmatched = false
customerFile:
"customer_id,name"
"1,Alice"
"2,Bob"
processorFile:
"customer_id,amount"
"1,10"
"1,20"
Expected output (conceptually)
Header: "customer_id,name,customer_id,amount"
Rows:
"1,Alice,1,10"
"1,Alice,1,20"
"2,Bob,,"
Example
Input
customer_id
customer_id,name
1,Alice
2,Bob
processor_id
customer_id,amount
1,10
1,20
Output
(示意)该题为函数题,需在本地框架中调用 joinDataSet 验证输出行顺序与空列规则。