The box formatting part was fine, I got through it without too much trouble.
Start by clarifying edge cases like empty input, words longer than W, and multiple spaces. Then outline a greedy line-breaking algorithm: iterate through words, adding to current line if it fits, otherwise start a new line. Finally, construct the bordered output by padding each line to width W and surrounding with pipes.
Pro tip: Mention that this is essentially a simplified version of the text justification problem, and that you can achieve O(n) time and O(1) extra space (excluding output) by processing words in a single pass. Also, proactively discuss how you would handle words longer than W, as interviewers often check for this edge case.
Ask about empty strings, words longer than W, multiple spaces between words, and whether W includes the border characters. Confirm that words should not be broken.
Use a greedy approach: maintain a current line and its length. For each word, if adding it (plus a space if needed) exceeds W, finalize the current line and start a new one with the word. Otherwise, append it.
If a word is longer than W, decide whether to truncate, break it, or let it overflow. Typically, you should let it overflow or handle it as a special case. Also, ensure empty input produces an empty box or just borders.
For each line, pad it with spaces to reach width W (right-padding for the final line, and also for other lines since we only break at word boundaries). Then surround each line with '|' characters. Optionally, add top and bottom borders with '+' and '-'.
State that the algorithm runs in O(n) time where n is the total number of characters, and O(1) extra space (excluding output). Walk through a few test cases, including empty string, single word, and multiple lines.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.