Justify Text to a Fixed Width
Updated
Problem statement
Arrange the words in words into lines of exactly width characters. Every word is nonempty, contains only lowercase English letters, and fits within the width. Keep the words in input order and never split a word.
Use these formatting rules for this exercise:
- Put as many of the next words as possible on each line, allowing at least one ASCII space between neighboring words.
- For a line that is not the final line and contains at least two words, spread all its spaces as evenly as possible between the words. If some gaps need an extra space, give the extra spaces to the earlier gaps.
- Left-align the final line and every line containing just one word: put one space between words, then add spaces at the end until the line has exactly
widthcharacters.
Return the lines as a string array. Keep the trailing spaces in each returned string. An empty input returns an empty array. Do not change the input words.
Function
justifyText(words: String[], width: int) → String[]Examples
Example 1
words = ["this","is","text","justified"]width = 16return = ["this is text","justified "]The first line has six spaces across two gaps: this followed by three spaces, is, three spaces and text. The final nine-letter word receives seven trailing spaces.
Example 2
words = ["a","b","c","d"]width = 3return = ["a b","c d"]Each greedy line holds two words. Both returned strings have length three.
Example 3
words = ["word"]width = 8return = ["word "]The returned word has four trailing spaces; those spaces are part of the result.
Constraints
- For this exercise, assume
0 <= words.length <= 100. - For this exercise, assume
1 <= width <= 80. - Each word has from
1through20lowercase English letters and length at mostwidth.
More Coinbase interview questions
- Select Transactions with Child-Pays-for-Parent PackagesPHONE SCREEN · Seen Oct 2026
- Lexicographically Smallest Topological OrderPHONE SCREEN · Seen Apr 2022
- Ordered Badge Entry and Exit ViolationsPHONE SCREEN · Seen Feb 2022
- Longest Common Contiguous SubarrayPHONE SCREEN · Seen Nov 2021
- Scheduled Payments with Account AliasesONSITE INTERVIEW · Seen Oct 2026
- Task Management SystemOA · Seen Sep 2026
- Cloud Compute Credits SystemOA · Seen Aug 2026
- Dynamic Fixed-K Score LeaderboardONSITE INTERVIEW · Seen Aug 2026
- Minimum Days for Fixed-Order Tasks with CooldownONSITE INTERVIEW · Seen Aug 2026
- Sliding-Window Averages as Reduced FractionsONSITE INTERVIEW · Seen Aug 2026
Similar questions at other companies
- Largest Number Below N from Reusable DigitsByteDance · PHONE SCREENByteDance interview questions
- Remove K DigitsProcol · PHONE SCREEN
- Reorderable Task Scheduler With CooldownWaymo · PHONE SCREENWaymo interview questions
- Reorganize a StringAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Valid Parenthesis StringOracle · PHONE SCREENOracle interview questions
- Shortest Way to Form a Target StringPinterest · PHONE SCREENPinterest interview questions
- Maximize Workday EarningsCitadel · OA · PHONE SCREEN · ONSITE INTERVIEWCitadel interview questions
- Minimum Removal for Balanced ParenthesesMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Longest Dictionary TokenizationGoogle · PHONE SCREENGoogle interview questions
- Biggest Number From Gluing Numbers TogetherAdobe · PHONE SCREENAdobe interview questions