I've seen this word-wrap question in two interviews and haven't been able to solve it either time. Most recently at Sentry. I'm guessing there's a really niche edge case that makes building lines back up tricky.
Text Formatter
Given a string containing a sequence of words separated by whitespace-like characters, add underscores so that the words can be printed neatly given a character limit per line. Assume that there are no words that exceed the line's width.
Your solution will take as input:
- A string of words separated by whitespace-like characters
- An integer representing the length which each line in the output must be
If there is any amount or type of whitespace between two words in the input string, they are considered separate words with the exception of hyphenated words (more information below about how to treat hyphenated words).
For example, "hello world" and "hello world" lead to the same output.
Consider the following sample input:
"a cat is an animal"
6
The function should output the following:
['a__cat', 'is__an', 'animal']
Each element in the output is a line, each line has 6 characters (including underscores).
Even Underscore Distribution
There's a catch: underscores should be distributed evenly between words in a line. When not possible, the extra underscores should be appended to the end of the line.
Therefore, given the following input:
"cat is an animal and so is a dog"
12
Your function should return:
['cat__is__an_', 'animal___and', 'so_is_a_dog_']
In this example, "cat__is__an_" has two underscores between each word because the words themselves have 7 characters, which leaves 5 underscores to be added. This allows us to add two underscores between each word, with one underscore left over to be added to the end because it couldn't be evenly added between the words.
Line two, "animal___and", can only contain two words, so we are allowed to add all the underscores between the two words.
Single Word Lines
If any of the lines end up having only 1 word on them, then instead of appending the extra underscores to the end of the line, we want to center the word. If the word can't be exactly centered, as before, the extra underscore should be added to the end of the line.
For example, the input:
"human!"
8
Will output:
["_human__"]
There are 3 underscores that need to be added so the 1 extra underscore goes to the end of the line.
Hyphenated Words
Hyphenated words ("self-inflicted", "pre-approval", "mother-in-law") should be kept on one line if possible. If the first part of the hyphenated word including the hyphen ("self-" from "self-inflicted") fits while the whole word doesn't, then the word should be split up.
Hyphenated words can have any amount of whitespace around the hyphens, but they should be treated as if there is no white space around the hyphens. For example, an input of "self-inflicted" and "self - inflicted" should lead to the same result.
For example, given the following input:
"auto-complete is my go - to"
8
Your function should return ['_auto-__', 'complete', 'is____my', '_go-to__']
In this example, "auto-complete" cannot fit on the line, so we split it up and have "auto-" on the first line. Even with the extra whitespace, we treat it as "go-to", which fits on one line so there is no need to split it up.


