Wrap Words into Hyphen-Separated Lines
Updated
Problem statement
You are given a list of words in order and a number maxLen. Wrap the words into lines. Never split a word, and keep the words in their original order.
Each line joins words with exactly one hyphen between them. Put as many words in a row on each line as fit within maxLen. If adding the next word and the hyphen before it would make the line longer than maxLen, start a new line with that word.
Every word is a nonempty string of English letters, and each word fits on a line by itself. Keep the original upper and lower case, and keep repeated words. Do not add a hyphen at the end of a line or any padding. If there are no words, return an empty array.
Function
wrapWords(words: String[], maxLen: int) → String[]Examples
Example 1
words = ["Hello","Sir","Please","Upvote","If","You","Like","My","Post"]maxLen = 10return = ["Hello-Sir","Please","Upvote-If","You-Like","My-Post"]Hello-Sir fits, but adding -Please would be longer than maxLen. So Please starts a new line, and Please-Upvote is too long too. Upvote-If fits, but adding -You does not. You-Like fits, but adding -My does not. My-Post ends the list. That gives five lines: Hello-Sir, Please, Upvote-If, You-Like and My-Post.
Example 2
words = ["a","b","c"]maxLen = 3return = ["a-b","c"]a-b is exactly maxLen characters long, so it still fits. Adding -c would be too long, so c goes on its own line. The answer is ["a-b","c"].
Example 3
words = ["Abcd","e"]maxLen = 4return = ["Abcd","e"]Abcd fills the whole line of maxLen characters. Adding -e would be too long, so e starts a new line. No word is split and no line is padded, so the answer is ["Abcd","e"].
Constraints
0 <= words.length <= 2001 <= maxLen <= 100- Each word contains only uppercase or lowercase English letters and has length from 1 through maxLen.
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
- Maximize Binary XOR with a Set-Bit LimitJP Morgan Chase · PHONE SCREENJP Morgan Chase interview questions
- Minimum Insertions to Balance ParenthesesGoogle · PHONE SCREENGoogle interview questions
- Palindrome After At Most One DeletionAdobe · PHONE SCREENAdobe interview questions
- Wrap Words from Existing Text LinesCompass International Holdings · PHONE SCREENCompass International Holdings interview questions
- 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
- Greedy and Beam Sentence DecodingMicrosoft · PHONE SCREEN · ONSITE INTERVIEWMicrosoft interview questions
- Valid Parenthesis StringOracle · PHONE SCREENOracle interview questions