Wrap Text with Retained Boundary Spaces
Updated
Problem statement
Given text and maxLen, split the words into ordered lines without breaking a word. Put as many consecutive words as possible on each line, counting the letters and the single spaces between words toward the limit.
For this exercise, assume a line other than the last keeps exactly one trailing space from the input when its words and internal spaces use fewer than maxLen characters. If the group already fills maxLen, omit that following space. The final line has no trailing space. Add no other padding.
For this exercise, assume nonempty words contain printable ASCII characters other than space, exactly one ASCII space separates words, and input has no leading or trailing space. Every word individually fits; preserve punctuation, casing and repeated words. Empty input returns an empty array.
Function
wrapText(text: String, maxLen: int) → String[]Examples
Example 1
text = "Hey there. Hello world"maxLen = 10return = ["Hey there.","Hello ","world"]Note the result "Hello " with one trailing space.
Example 2
text = "aa b ccc d"maxLen = 5return = ["aa b ","ccc d"]This example retains one separator on a short nonfinal line, without adding any further padding.
Example 3
text = "Abcd e f"maxLen = 4return = ["Abcd","e f"]This example suppresses the separator after an exactly full line and leaves the final line unpadded.
Constraints
- For this exercise, assume
0 <= text.length <= 2000and1 <= maxLen <= 100. - Each word has length at most maxLen. Nonspace characters have ASCII codes from 33 through 126.
- Nonempty input uses exactly one space between words and has no leading or trailing space.
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