Wrap Words from Existing Text Lines
Updated
Problem statement
Given existing text lines and maxLen, first read all the words in order across the input lines, then wrap them into new lines. A break between input lines does not have to become an output line break.
For this exercise, assume each output line uses the largest consecutive group that fits with exactly one ASCII space between words. Add no trailing spaces or padding, preserve spelling and repeated occurrences, and return an empty array when there are no words.
For this exercise, assume every word contains only English letters and individually fits the maximum.
For this exercise, assume ASCII spaces separate input words; ignore leading, trailing and repeated spaces in input strings.
Function
wrapLines(lines: String[], maxLen: int) → String[]Examples
Example 1
lines = ["Hello Sir","Please Upvote","If You Like My Post"]maxLen = 10return = ["Hello Sir","Please","Upvote If","You Like","My Post"]Read the words in order and fill each line greedily; the input line breaks are ignored.
Example 2
lines = ["a bb"," c dd "]maxLen = 5return = ["a bb","c dd"]This example preserves word order and uses one space between words.
Example 3
lines = [""," ","Abcd e"]maxLen = 4return = ["Abcd","e"]This example has a fitting singleton and no line padding.
Constraints
- For this exercise, assume
0 <= lines.length <= 100and at most2000total input characters. - For this exercise, assume
1 <= maxLen <= 100. Each word has length from 1 through maxLen and contains only uppercase or lowercase English letters. - Input text may also contain ASCII spaces.
More Compass International Holdings Phone Screen questions
- Decode StringPHONE SCREEN · Seen Oct 2026
- Lowest Common Ancestors in a Parent DAGPHONE SCREEN · Seen Apr 2022
- Can One Dictionary Word Supply the Target Letters?PHONE SCREEN · Seen Apr 2022
- Greedy Grid Word Occurrences with No Reused CellsPHONE SCREEN · Seen Apr 2022
- Word Path Moving Only Right or DownPHONE SCREEN · Seen Apr 2022
- First Dictionary Word from Note LettersPHONE SCREEN · Seen Mar 2022
- Word Path Moving Only Up or RightPHONE SCREEN · Seen Feb 2022
- Top Five Stock Records in a Date RangePHONE SCREEN · Seen Oct 2021
- All Common Ancestors in a Parent GraphPHONE SCREEN · Seen Jan 2021
- Nodes With Zero or Three ParentsPHONE SCREEN · Seen Jan 2021
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
- Wrap Text with Retained Boundary SpacesAtlassian · PHONE SCREENAtlassian interview questions
- Palindrome After At Most One DeletionAdobe · PHONE SCREENAdobe 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