cswork / libraryOpen practice workspace

Coding questions · Kikoff

Parse CSV and format transactions

This question evaluates a candidate's ability to parse and format structured transactional data, emphasizing CSV parsing, trimming whitespace, numeric formatting to two decimal places, and preserving input row order.

Given a CSV file containing many transactional records with columns Merchant, Amount, Date (YYYY-MM-DD), and Status, write a script to parse the file and print, for each transaction, the following two-line block exactly in the given order:

---------------------------
|  {Merchant}               ${Amount with two decimals}  |
|  {Date}                   {Status}                     |
---------------------------

Example for one row:

---------------------------
|  Merchant                 $10.04                       |
|  2020-01-01               pending                      |
---------------------------

Requirements:

  • Preserve the input row order.
  • Format Amount as a dollar value with two decimals (e.g., $10. 04).
  • Trim surrounding whitespace in fields before formatting.
  • Output one block per transaction with the same delimiter lines and spacing pattern.

Format Transaction Records into Fixed Two-Line Blocks

You are given a table that stores transaction records imported from a CSV file. The CSV had columns Merchant, Amount, Date (YYYY-MM-DD), and Status. The import process has stored the rows in the table in their original order using an auto-incrementing primary key.

Write a SQL query that, for each transaction, outputs the following 4-line block as separate rows, preserving the original input order:

---------------------------
|  {Merchant}  ${Amount with two decimals}  |
|  {Date}  {Status}  |
---------------------------

Example for one transaction:

---------------------------
|  Merchant A  $10.04  |
|  2020-01-01  pending  |
---------------------------

Requirements:

  • Preserve the original row order using the primary key.
  • Format Amount as a dollar value with two decimals (e.g., $10.04, $5.00).
  • Trim surrounding whitespace in Merchant and Status before formatting.
  • Output one 4-row block per transaction, with the delimiter line '---------------------------' before and after the two content lines.
  • The result should have one row per printed line, with columns:
    • txn_id (the transaction id)
    • line_no (1, 2, 3, or 4 within each block)
    • line_text (the exact text of that line)

Table transactions

columntypeconstraints
txn_idINTPRIMARY KEY, NOT NULL
merchantVARCHAR(100)NOT NULL
amountDECIMAL(10,2)NOT NULL
txn_dateDATENOT NULL
statusVARCHAR(20)NOT NULL

Sample data:

amountmerchantstatustxn_datetxn_id
10.04Merchant Apending2020-01-011
5Store Bcompleted2020-01-022
123.4Shop Cfailed2020-01-033

Expected output

One row per printed line, showing the formatted 4-line block for each transaction, ordered by txn_id and line_no. Whitespace in merchant and status is trimmed, and amount is formatted as a dollar value with two decimals.

line_noline_texttxn_id
1---------------------------1
2| Merchant A $10.04 |1
3| 2020-01-01 pending |1
4---------------------------1
1---------------------------2
2| Store B $5.00 |2
3| 2020-01-02 completed |2
4---------------------------2
1---------------------------3
2| Shop C $123.40 |3
3| 2020-01-03 failed |3
4---------------------------3

Hints

  1. Use TRIM on merchant and status, and a formatting function like TO_CHAR (or equivalent in your SQL dialect) to ensure two decimal places on amount.
  2. Generate one row per printed line using VALUES/UNION ALL or a lateral join, and order by the transaction id and the line number within each block.