Print Sentences as Table
Company: Airbnb
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Implement a function that formats a list of sentences as a bordered table.
Given:
- `sentences`: a list of strings
- `width`: the fixed content width of each table row, such as `55`
Return the table as a list of strings, or print the lines in order.
Formatting rules:
1. Each sentence must appear on exactly one line; do not wrap text.
2. The horizontal border is `+` followed by `width` hyphens, followed by `+`.
3. Each content row is `|` followed by the sentence left-aligned and padded with spaces to exactly `width` characters, followed by `|`.
4. Print a horizontal border before the first row, between every pair of rows, and after the last row.
Example with a smaller width for readability:
Input:
```text
sentences = ["Hello world", "How are you", "Bye"]
width = 15
```
Output:
```text
+---------------+
|Hello world |
+---------------+
|How are you |
+---------------+
|Bye |
+---------------+
```
Edge cases to handle:
- If a sentence is longer than `width`, report invalid input, for example by throwing an exception.
- An empty sentence should produce a blank padded row.
- An empty list should produce no output.
Quick Answer: This question evaluates string manipulation, precise output formatting, input validation, and attention to edge cases for fixed-width table rendering. It is commonly asked in Coding & Algorithms interviews because it verifies adherence to exact specifications and robust handling of corner cases; the domain is coding/algorithms with a focus on practical application rather than abstract theory.
Implement a function that formats a list of sentences as a bordered table and returns the table as a list of strings.
Given a list of strings `sentences` and an integer `width`, build the table using these rules:
1. Each sentence must appear on exactly one row. Do not wrap text.
2. The horizontal border is `+` followed by `width` hyphens, followed by `+`.
3. Each content row is `|` followed by the sentence left-aligned and padded with spaces to exactly `width` characters, followed by `|`.
4. Place a horizontal border before the first row, between every pair of rows, and after the last row.
5. If any sentence is longer than `width`, raise a `ValueError`.
6. An empty sentence should produce a blank padded row.
7. If `sentences` is empty, return an empty list.
Example:
If `sentences = ["Hello world", "How are you", "Bye"]` and `width = 15`, the returned list should represent:
+---------------+
|Hello world |
+---------------+
|How are you |
+---------------+
|Bye |
+---------------+
Constraints
- 0 <= len(sentences) <= 10^4
- 0 <= width <= 10^4
- Each element of `sentences` is a string
- If any sentence has length greater than `width`, the function must raise `ValueError`
Examples
Input: (['Hello world', 'How are you', 'Bye'], 15)
Expected Output: ['+---------------+', '|Hello world |', '+---------------+', '|How are you |', '+---------------+', '|Bye |', '+---------------+']
Explanation: Each sentence is placed on its own padded row, with a border before, between, and after rows.
Input: (['', 'A'], 3)
Expected Output: ['+---+', '| |', '+---+', '|A |', '+---+']
Explanation: An empty sentence becomes a fully blank padded row, and 'A' is padded to width 3.
Input: ([], 5)
Expected Output: []
Explanation: An empty input list should produce no output lines.
Input: (['abc', 'x y'], 3)
Expected Output: ['+---+', '|abc|', '+---+', '|x y|', '+---+']
Explanation: Both sentences fit exactly into the given width, so no extra padding is needed.
Input: ([''], 0)
Expected Output: ['++', '||', '++']
Explanation: With zero content width, the border has no hyphens and the empty sentence fits exactly.
Hints
- Build the border string once as `'+' + '-' * width + '+'` and reuse it.
- For each sentence, left-align it to `width` characters before surrounding it with `|` characters.