The greedy packing part is fine, you just keep adding words until the next one doesn't fit.
Clarify the problem constraints and edge cases first, then outline a greedy line-by-line approach: pack as many words as fit, distribute spaces evenly (with extra spaces on the left), and handle the last line separately. Walk through a small example to validate the logic, then discuss complexity and potential optimizations.
Pro tip: Mention that you'd write helper functions for space distribution and line justification to keep the code clean and testable, and explicitly handle edge cases like a single word per line and the last line.
Ask about input constraints (e.g., word length vs. maxWidth), whether words can be longer than maxWidth, and how to handle the last line. Confirm that spaces must be distributed as evenly as possible, with extra spaces going to the leftmost gaps.
Iterate through words, adding them to the current line as long as the total length (including at least one space between words) does not exceed maxWidth. Once the next word doesn't fit, finalize the current line.
For non-last lines, compute the total spaces needed and distribute them as evenly as possible. If spaces don't divide evenly, assign the extra spaces to the leftmost gaps. Handle the case of a single word by left-justifying it.
Left-justify the last line by joining words with a single space and padding the end with spaces to reach maxWidth. Also handle cases where a word is longer than maxWidth (though typically not allowed) and empty input.
State that the time complexity is O(n) where n is the total number of characters, as each word is processed once. Walk through a test case to verify correctness, and mention potential optimizations or trade-offs.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.