Fixed-Width Text Display
Otter.AiSoftware Engineer · Technical ScreenLast updated
This question tests a candidate's ability to implement greedy line-packing and space-distribution algorithms, core string manipulation skills evaluated in software engineering interviews. It assesses practical application of text layout logic, including handling edge cases like single-word lines and justified formatting with uneven space distribution.
Fixed-Width Text Display
You are building a text display that renders a sequence of words inside a viewport of a fixed character width maxWidth. The display lays words out left to right, packing as many words as possible onto each line before wrapping to the next line.
The layout rule is the standard one used by word-wrapping renderers: when placing words on a line, consecutive words must be separated by at least one space. A word may be placed on the current line only if, after adding it (together with the single spaces that separate it from the words already on the line), the line's length does not exceed maxWidth. Otherwise the word starts a new line.
You are given a list of words words and an integer maxWidth. Every individual word satisfies 1 <= word.length <= maxWidth, so every word fits on a line by itself.
This problem has two parts.
Part 1 — Words per line (simplified)
Using the packing rule above with exactly one space between adjacent words, return a list of integers where the i-th element is the number of words placed on the i-th line.
Concretely, greedily fill each line: a word fits on the current line if currentLength + 1 + word.length <= maxWidth, where currentLength is the length of the words already on the line plus the single spaces between them (an empty line has currentLength = 0 and the first word on a line never needs a leading space). When a word does not fit, it begins a new line.
Example
words = ["This", "is", "an", "example", "of", "text", "display"]
maxWidth = 16
Lines:
"This is an" (10 chars) -> 3 words
"example of text" (15 chars) -> 3 words
"display" (7 chars) -> 1 word
Output: [3, 3, 1]
Part 2 — Full justification
Now render the actual lines as fully justified text, where every line is exactly maxWidth characters wide. Pack words greedily into lines using the same rule as Part 1 (at least one space between words), then format each line as follows:
- Distribute extra spaces between words so that the line is exactly
maxWidthcharacters. If the spaces on a line do not divide evenly between the word gaps, the left gaps receive more spaces than the gaps on the right. - A line that contains only one word, and the last line of the output, are left-justified: words are separated by a single space and the remaining characters on the right are padded with spaces to reach
maxWidth.
Return the list of formatted lines, each a string of length exactly maxWidth.
Example
words = ["This", "is", "an", "example", "of", "text", "justification"]
maxWidth = 16
Output:
[
"This is an",
"example of text",
"justification "
]
(The first line has two gaps and 4 extra spaces, split 2/2; the second line has two gaps and 2 extra spaces, split 1/1; the last line is left-justified and right-padded.)
Input / Output
words: a list of strings, each containing no spaces, with1 <= words.lengthand1 <= word.length <= maxWidth.maxWidth: a positive integer.- Part 1 returns a list of integers (word counts per line).
- Part 2 returns a list of strings, each of length exactly
maxWidth.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.