Parse and Filter Chargeback Disputes
Problem statement
Implement parseChargebacks(rows) to parse dated network chargeback files, ignore invalid data rows, and remove disputes withdrawn in a strictly later file. Return the remaining dispute rows. The requirements build on one another: Part 1 parses and formats valid records; Part 2 filters invalid records; Part 3 also handles withdrawals. Implement the complete Part 3 behavior.
File layout
The array flattens 1 through 10 files. Each file begins with NETWORK_yyyymmdd.csv, then a header, then at most 50 data rows. NETWORK is always uppercase and is one of VISA, PAYPAL, AMEX, or JCB. Files may appear in any date order. A new filename starts the next file.
Every header contains exactly these six names in any order:
transaction,merchant,amount,currency,evidence_due_by,reasonInterpret each data row using its own file's header. transaction identifies the disputed transaction on that network; merchant identifies its merchant; amount is in minor currency units; currency is the currency code; evidence_due_by is the merchant's evidence deadline as a UNIX timestamp; reason identifies the reason for the dispute.
Invalid rows
Ignore a data row if its currency is unsupported, its amount is not a positive integer, or its evidence deadline is not a whole number. Supported currencies are EUR, USD, SGD, BRL, JPY, ISK, and KRW. Ignore only that row.
For this practice version, a whole-number field must be a nonempty sequence of ASCII digits. Leading zeros are allowed. A valid amount is from 1 through 1000000000, and a valid deadline is from 0 through 9223372036854775807, inclusive. Out-of-range values are invalid. Do not trim fields; signs, spaces, and decimal points are not digits.
Withdrawals
A valid row whose reason is exactly withdrawn is a withdrawal; every other valid row is a normal dispute. A valid withdrawal cancels a normal dispute only when their network and transaction ID match and the withdrawal's filename date is strictly later than the normal row's filename date. The evidence deadline and the order of files in the array do not determine cancellation.
An invalid withdrawal, or a withdrawal with no earlier matching normal dispute on that network, is ignored. An earlier or same-date withdrawal does not cancel a normal dispute. If both rows are invalid, ignore both. Never return a withdrawal row.
A transaction may appear in at most two files for each network and may be withdrawn at most once. For this practice version, repeated normal rows within a file are allowed: keep every valid normal row unless a qualifying withdrawal cancels it. A withdrawal can therefore cancel several earlier normal rows.
Output
Return one string for each surviving normal row, with four comma-separated fields in this order:
dispute_id,merchant,value,evidence_due_bydispute_idconcatenates the uppercase network and the original transaction ID without a separator.- Copy the merchant and the deadline text from that normal row exactly, including any leading zeros in the deadline.
- For
EUR,USD,SGD, andBRL, divide the amount by100to get major units. ForJPY,ISK, andKRW, one minor unit is one major unit. - Always use a period and exactly two digits after it, then append the currency code. For example,
1minor unit becomes0.01USDor1.00JPY.
Sort by the concatenated dispute ID in ascending lexicographic order. For this practice version, equal IDs preserve the original data-row order. Return an empty array if no normal disputes remain.
Function
parseChargebacks(rows: String[]) → String[]Examples
Example 1
rows = ["VISA_20230601.csv","transaction,merchant,amount,currency,evidence_due_by,reason","123890132,47821,37906,USD,1686812400,fraudulent","110450953,63724,12750,JPY,1686898800,duplicate","JCB_20230604.csv","transaction,merchant,currency,amount,evidence_due_by,reason","110450953,11000,SGD,15000,1686898820,duplicate"]return = ["JCB110450953,11000,150.00SGD,1686898820","VISA110450953,63724,12750.00JPY,1686898800","VISA123890132,47821,379.06USD,1686812400"]The second file reorders currency and amount. The values are 150.00SGD, 12750.00JPY, and 379.06USD. Sort the results by the concatenated dispute ID.
Example 2
rows = ["VISA_20230601.csv","transaction,merchant,amount,currency,evidence_due_by,reason","123890132,47821,37906,USD,1686812400,fraudulent","110450953,63724,12750,CAD,1686898800,duplicate","138843997,57101,99.95,EUR,1686898800,product_not_received"]return = ["VISA123890132,47821,379.06USD,1686812400"]CAD is unsupported, and 99.95 is not a whole-number amount. Ignore those rows independently; only the valid USD dispute remains.
Example 3
rows = ["VISA_20230602.csv","transaction,merchant,amount,currency,evidence_due_by,reason","123890132,47821,37906,USD,1668812400,fraudulent","110450953,63724,12750,JPY,1668898800,withdrawn","VISA_20230601.csv","transaction,merchant,currency,amount,evidence_due_by,reason","110450953,63724,JPY,12750,1668898800,duplicate","123890132,47821,USD,37906,1668812400,withdrawn"]return = ["VISA123890132,47821,379.06USD,1668812400"]The June 2 valid JPY withdrawal cancels the June 1 JPY dispute. The June 1 USD withdrawal is earlier than the June 2 USD dispute and cannot cancel it. The surviving USD dispute keeps its original deadline, 1668812400.
Constraints
- There are
1through10files, each with at most50data rows. - Every filename has the stated network and a valid eight-digit calendar date. Every header is a permutation of the six stated field names.
- Each
(network, transaction)pair appears in at most two file blocks. At most one data row for that pair has reason exactlywithdrawn. This is a file bound, not a two-row bound. - For this practice version, every data row has exactly six comma-separated fields;
transaction,merchant, andreasonare nonempty. Fields are not quoted and contain no comma or embedded line ending. - For this practice version, fields contain printable ASCII characters from
U+0020throughU+007E, excluding double quote, backslash,<,>, and&. No whitespace trimming is performed. - For this practice version, all strings in
rowstogether contain at most800UTF-8 bytes, including filenames, headers, and delimiters. Data-row strings together contain at most500UTF-8 bytes. These are additional practice limits.