Format CSV Rows as an ASCII Table
Updated
Problem statement
The array rows contains an in-memory CSV table. Every row has the same number of comma-separated fields. Fields use the simple unquoted CSV subset described in the constraints.
Format the data as a readable ASCII table:
- Set each column's width to the longest field in that column.
- Left-align every field and pad it with spaces to its column width.
- Put one space on each side of every field, separate columns with
|, and put|at both ends.
Return one formatted string per input row. Do not add horizontal border rows.
Function
formatCsvTable(rows: String[]) → String[]Examples
Example 1
rows = ["name,age","Ada,36","Grace,37"]return = ["| name | age |","| Ada | 36 |","| Grace | 37 |"]In the first column, Grace is the longest field, so the width is five. In the second column, age is the longest field, so the width is three. Shorter fields get spaces on the right: name gets one, Ada gets two, and each age number gets one. Then every field gets one space on each side, with | between the columns and at both ends. That gives the three rows in the output.
Example 2
rows = ["a,b","long,x","mid,xyz"]return = ["| a | b |","| long | x |","| mid | xyz |"]In the first column, long from the second row is the longest field, so the width is four. In the second column, xyz from the third row is the longest field, so the width is three. So a and mid are padded to four characters, and b and x are padded to three. Adding the side spaces and pipes gives the three rows in the output.
Example 3
rows = ["solo","x"]return = ["| solo |","| x |"]The table has one column, and solo sets its width to four. So x gets three spaces on its right. Each row still has a | at both ends, which gives the two rows in the output.
Constraints
1 <= rows.length <= 1000.- Every row has between
1and20fields, and all rows have the same number of fields. - Each field has length at most
50and contains only ASCII letters, digits, and spaces. - Fields may be empty but have no leading or trailing spaces.
- Fields do not contain commas, quotes, pipes, or line breaks.
More Amazon Phone Screen questions
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Spread Through a GridPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Consistent-Hash Packet RouterPHONE SCREEN · Seen Nov 2026
- Count Connected ComponentsPHONE SCREEN · Seen Nov 2026
- Second Largest Distinct ElementPHONE SCREEN · Seen Nov 2026
- Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent ElementsOA · PHONE SCREEN · Seen Nov 2026
- Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Validate Typed CSV RecordsStripe · PHONE SCREENStripe interview questions
- Machine Message RouterOpenAI · PHONE SCREENOpenAI interview questions
- Reversible Run-Length EncodingZillow · PHONE SCREEN
- Correct Apartment Bedroom CountsRamp · OARamp interview questions
- Parse Command-Line TokensSamsara · ONSITE INTERVIEWSamsara interview questions
- Search and Format ContactsAdyen · PHONE SCREENAdyen interview questions
- Match a Variadic Function SignatureConfluent · PHONE SCREENConfluent interview questions
- Process Refunds with Bounded RetriesMercor · PHONE SCREENMercor interview questions
- Restricted Decimal Number ValidatorLinkedIn · PHONE SCREENLinkedIn interview questions
Amazon interview experiences
- Amazon Experienced SDE Onsite Questions: 1 Experience
- Amazon SDE Onsite Interview Questions: 9 Experiences
- Amazon SDE II and L5 FDE Interview Questions: 4 Experiences
- Amazon Full-Time OA Problems, Earlier Reports (Part 5 of 5)
- Amazon Full-Time OA Problems, Recent Reports (Part 4 of 5)
- Amazon New Grad OA Problems: Every Task We Have (Part 3 of 5)