Text Justification
Updated
Problem statement
Given an ordered array of nonempty strings words and an integer maxWidth, return lines of exactly maxWidth characters.
For this exercise, use the following formatting rules:
- Keep the words in input order and never split a word. Put as many consecutive words as possible on each line, allowing at least one space between adjacent words.
- For each nonfinal line with two or more words, distribute all spaces as evenly as possible between words. When some gaps need an extra space, give it to the gaps farther left.
- Left-align the final line and every line containing only one word: use one space between adjacent words, then add trailing spaces to reach
maxWidth.
Trailing spaces are part of the returned strings.
Function
fullJustify(words: String[], maxWidth: int) → List<String>Examples
Example 1
words = ["a","bb","ccc","dddd","ee"]maxWidth = 10return = ["a bb ccc","dddd ee "]The first line contains six word characters and four spaces, split into two gaps. The final line has one internal space and three trailing spaces.
Example 2
words = ["longword","a","b"]maxWidth = 8return = ["longword","a b "]The first word fills its line. The final line contains three characters before five trailing spaces.
Example 3
words = ["hi"]maxWidth = 4return = ["hi "]The single final word receives two trailing spaces.
Constraints
- For this exercise, assume
1 <= words.length <= 200and1 <= maxWidth <= 80. - Each word contains printable ASCII characters without spaces.
- For this exercise, assume
1 <= words[i].length <= min(20, maxWidth).
More WhatNot Phone Screen questions
- Merge Sorted Squares of Two ArraysPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Adjacent Pair and Run RemovalPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- User Journey PathsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- User Journey Paths from a Log StreamPHONE SCREEN · Seen Nov 2026
- Reachable Exits in an Obstacle GridPHONE SCREEN · Seen Oct 2026
- Palindrome with One DeletionPHONE SCREEN · Seen Oct 2026
- Search a 2D MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Attraction Route Without Reusing TrailsPHONE SCREEN · Seen Sep 2026
- Earliest Car for Each PassengerPHONE SCREEN · Seen Sep 2026
- Minimum Stores for a Shopping ListPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Text JustificationGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle 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
- 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
- Remove K DigitsProcol · PHONE SCREEN