Text Justification
Updated
Problem statement
Given an array of non-empty strings words and an integer maxWidth, format the words into lines of exactly maxWidth characters.
Greedily place as many words as possible on each line. For every non-final line containing multiple words, distribute spaces as evenly as possible; when they do not divide evenly, gaps farther left receive one extra space. A non-final line containing one word is padded on the right.
The final line is left-justified with one space between adjacent words and trailing spaces added to reach maxWidth. Return the formatted lines.
Function
fullJustify(words: String[], maxWidth: int) → List<String>Examples
Example 1
words = ["This", "is", "an", "example", "of", "text", "justification."]maxWidth = 16return = ["This is an", "example of text", "justification. "]The greedy grouping produces three lines of exactly 16 characters. Spaces on non-final lines are distributed evenly, while the final line is padded on the right.
Example 2
words = ["What","must","be","acknowledgment","shall","be"]maxWidth = 16return = ["What must be", "acknowledgment ", "shall be "]The one-word middle line is padded on the right. The final line uses one space between its words and trailing spaces to reach width 16.
Example 3
words = ["Science","is","what","we","understand","well","enough","to","explain","to","a","computer.","Art","is","everything","else","we","do"]maxWidth = 20return = ["Science is what we","understand well","enough to explain to","a computer. Art is","everything else we","do "]Each line has exactly 20 characters. Extra spaces on non-final lines are assigned to the leftmost gaps, and the final line is right-padded.
Constraints
1 <= words.length <= 3001 <= words[i].length <= 201 <= maxWidth <= 100words[i].length <= maxWidth- Every word contains only English letters and symbols.
More Google Phone Screen questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Periodic-Color Binary Forest RootsPHONE SCREEN · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
- Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
- Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
- Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Wildcard MatchingTripjack · ONSITE INTERVIEWTripjack interview questions
- Greedy and Beam Sentence DecodingMicrosoft · PHONE SCREEN · ONSITE INTERVIEWMicrosoft interview questions
- Remove Duplicate Letters for the Largest ResultAmazon · PHONE SCREEN
- Text JustificationUber · PHONE SCREENUber interview questions
- Text JustificationWhatNot · PHONE SCREENWhatNot interview questions
- Word Wrap with Even JustificationSentry · OA · PHONE SCREENSentry interview questions
- Match a Pattern With ? and *Adobe · PHONE SCREENAdobe interview questions
- Lexicographically Largest Distinct-Letter SubsequenceNavan · PHONE SCREENNavan interview questions
- Text JustificationBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Largest Number Below N from Reusable DigitsByteDance · PHONE SCREENByteDance interview questions