Render paragraphs with independent word-wrapping widths inside one ASCII box, preserving word order while padding every line to the largest requested interior width.
## Problem
Render several paragraphs inside one ASCII box. Each paragraph has its own wrapping width. Words must remain intact and appear in their original order. Lines produced for a paragraph may contain at most that paragraph's width in characters, excluding box borders.
The box interior width is the largest wrapping width among the paragraphs. Pad every rendered line with trailing spaces to that interior width, surround it with `|`, and use a top and bottom border made of `+`, `-`, and `+`. Insert no blank line between paragraphs.
### Function Contract
Implement `renderBox(paragraphs, widths)`, where `paragraphs[i]` is a string and `widths[i]` is its positive wrapping width. Return the complete box as an array of lines.
### Constraints & Assumptions
- `1 <= len(paragraphs) == len(widths) <= 100`.
- Each paragraph contains words separated by one or more spaces.
- Every word length is at most its paragraph's width.
- Leading, trailing, and repeated input spaces are normalized to one separator between output words.
- The total number of input characters is at most `200,000`.
- Greedy wrapping is required: place as many whole words as fit on each line.
### Clarifying Questions to Ask
- May a word be split? No.
- Does padding count toward the paragraph's wrapping width? No; padding only aligns the shared box.
- What determines the box width? The maximum value in `widths`.
- Are empty paragraphs allowed? No.
```hint Finish one paragraph at a time
For each paragraph, maintain the current line length. Add the next word only if the existing content, one separator, and that word fit its assigned width.
```
```hint Separate wrapping from framing
First create the unpadded content lines. Then pad each one to the common interior width and add borders.
```
### Example
```text
paragraphs = ["red green blue", "one two three four"]
widths = [10, 7]
result = [
"+----------+",
"|red green |",
"|blue |",
"|one two |",
"|three |",
"|four |",
"+----------+"
]
```
### Evaluation Focus
- Applies greedy word wrapping independently using each paragraph's width.
- Preserves word and paragraph order.
- Pads to the maximum width without changing wrapping decisions.
- Produces exact borders and no extra blank lines.
- Runs in linear time in the input and output size.
### Extensions to Discuss
1. How would you support left, right, and center alignment per paragraph?
2. What policy would you choose for a word longer than its assigned width?
3. How could the renderer emit lines lazily instead of materializing the full box?
Quick Answer: Render paragraphs with independent word-wrapping widths inside one ASCII box, preserving word order while padding every line to the largest requested interior width.
Render several paragraphs inside one ASCII box. Each paragraph has its own wrapping width. Words must remain intact and appear in their original order. Lines produced for a paragraph may contain at most that paragraph's width in characters, excluding box borders.
The box interior width is the largest wrapping width among the paragraphs. Pad every rendered line with trailing spaces to that interior width, surround it with |, and use a top and bottom border made of +, -, and +. Insert no blank line between paragraphs.
Function Contract
Implement renderBox(paragraphs, widths), where paragraphs[i] is a string and widths[i] is its positive wrapping width. Return the complete box as an array of lines.
Constraints & Assumptions
1 <= len(paragraphs) == len(widths) <= 100
.
Each paragraph contains words separated by one or more spaces.
Every word length is at most its paragraph's width.
Leading, trailing, and repeated input spaces are normalized to one separator between output words.
The total number of input characters is at most
200,000
.
Greedy wrapping is required: place as many whole words as fit on each line.
Clarifying Questions to Ask Guidance
May a word be split? No.
Does padding count toward the paragraph's wrapping width? No; padding only aligns the shared box.
What determines the box width? The maximum value in
widths
.
Are empty paragraphs allowed? No.
Example
paragraphs = ["red green blue", "one two three four"]
widths = [10, 7]
result = [
"+----------+",
"|red green |",
"|blue |",
"|one two |",
"|three |",
"|four |",
"+----------+"
]
Evaluation Focus
Applies greedy word wrapping independently using each paragraph's width.
Preserves word and paragraph order.
Pads to the maximum width without changing wrapping decisions.
Produces exact borders and no extra blank lines.
Runs in linear time in the input and output size.
Extensions to Discuss
How would you support left, right, and center alignment per paragraph?
What policy would you choose for a word longer than its assigned width?
How could the renderer emit lines lazily instead of materializing the full box?