Reflow Text with Balanced Hyphens
Updated
Problem statement
Read the words from the strings in text in their original order, then arrange them into new lines no wider than width. Never split a word. Select the largest consecutive group of words that fits when separated by one hyphen.
Distribute hyphens between words as evenly as possible to reach the width on each fully justified line. Gap lengths may differ by at most one; put any extra separator characters in the leftmost gaps.
Leave the final line without padding, with one hyphen between its words. In this exercise, also leave every line containing only one word without padding, even if it is not the final line.
For this exercise, assume input contains only English letters and ASCII spaces. Ignore leading, trailing and repeated spaces inside source strings; source-string boundaries act as whitespace. An empty input or input without words returns an empty array.
Function
justifyHyphenText(text: String[], width: int) → String[]Examples
Example 1
text = ["The day began as still as the","night abruptly lighted with","brilliant flame"]width = 24return = ["The--day--began-as-still","as--the--night--abruptly","lighted--with--brilliant","flame"]With width 24, all three justified strings are exactly 24 characters and flame is unchanged.
Example 2
text = ["a bb ccc d e f"]width = 6return = ["a---bb","ccc--d","e-f"]This example leaves the final multiword line e-f unpadded.
Example 3
text = ["abcdefg a b"]width = 8return = ["abcdefg","a-b"]This example shows the rule that a nonfinal line with a single word is not padded.
Constraints
- For this exercise, assume
0 <= text.length <= 100and the total number of input characters is at most2000. - For this exercise, assume
1 <= width <= 100, and every nonempty word has length at most width. - Each input string contains only uppercase or lowercase English letters and ASCII spaces.
More Atlassian Phone Screen questions
- Merge Overlapping IntervalsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Tennis Court SchedulingPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Implement a Rate LimiterPHONE SCREEN · Seen Oct 2026
- Longest Consistent Activity StreakPHONE SCREEN · Seen Sep 2026
- Music Player Operation SequencePHONE SCREEN · Seen Sep 2026
- Underline Shortest Unique SubstringsPHONE SCREEN · Seen Aug 2026
- Can a Dictionary Word Be Formed from Note Letters?PHONE SCREEN · Seen Feb 2026
- Word Path Moving Only Right or DownPHONE SCREEN · Seen Feb 2026
- Ordered Badge Entry and Exit ViolationsPHONE SCREEN · Seen Aug 2025
- Two Sum with an Ordered Index PairPHONE SCREEN · Seen Aug 2025
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