Align Wrapped Paragraph Lines
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt on this ZipRecruiter OA, reported in September 2024, is treating the padding as part of the wrapping. They're two separate steps. The problem is simulation with greedy line wrapping: for each row, pack words with single spaces until the next word would push past width, then align. LEFT pads on the right, RIGHT pads on the left. It looks like string busywork, and it is, but the edge cases are where points leak. If you blank on those, StealthCoder is a quiet safety net during the live OA.
The problem
Each row of lines is a sequence of words, paired with LEFT or RIGHT. Greedily wrap the row using one space between words so each content line is at most width. Every word fits. Pad each wrapped line with spaces to exactly width on the opposite side of its alignment. An empty word row emits one blank line. Return all rendered lines in row order. Function alignParagraphLines(lines: String[][], alignments: String[], width: int) → String[] Examples Example 1 lines = [["hello","world"],["right"]] alignments = ["LEFT","RIGHT"] width = 11 return = ["hello world"," right"] Rows are padded on opposite sides. Example 2 lines = [["aa","bb","cc"]] alignments = ["LEFT"] width = 5 return = ["aa bb","cc "] The final word moves to a second line. Constraints lines.length == alignments.length 1 <= width <= 1000 The total word length is at most 100000.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to keep the wrap and the pad apart. Walk each row, keep a current line and its length. If current length plus 1 plus the word length fits in width, append it. Otherwise flush the line and start a new one. Flush the last line too. Then pad: LEFT means line plus spaces, RIGHT means spaces plus line, to exactly width. The pitfalls are the empty row, which must emit one blank line of width spaces (padded the same way), and the first word on a line, where you add no leading space. Don't pad before you've finished wrapping. Building strings by repeated concatenation is also risky with 100000 total characters, so collect pieces in a list and join. Runtime is linear in total characters. If you freeze on the empty row or the off-by-one in the fit check, StealthCoder is the hedge for the live OA.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Align Wrapped Paragraph Lines cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as text justification. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass ZipRecruiter's OA.
ZipRecruiter reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Align Wrapped Paragraph Lines FAQ
How hard is this ZipRecruiter question really?+
Easy to medium. There's no clever algorithm, just careful simulation. The difficulty is in the details: the fit check, the empty row, and padding on the correct side. If you trace both examples by hand before coding, you'll likely pass.
What's the trick to the greedy wrap?+
Track the current line length. A word fits if current length + 1 + word length is at most width, or if the line is empty and you just place it. Otherwise flush and start fresh. Every word fits alone, so you never need to split a word.
How should I handle an empty word row?+
The statement says an empty row emits one blank line. Treat it as a single empty line, then pad it to width like any other. Which side you pad doesn't change the result, since the line is all spaces.
Does the alignment mean where the text sits or where the padding goes?+
Text sits on the named side, padding goes on the opposite side. LEFT puts the text first and the spaces after. RIGHT puts the spaces first. Example 1 shows this: ' right' is padded on the left to width 11.
How do I prepare for this in 48 hours?+
Write a wrap-and-pad function from scratch twice, and test it with an empty row, a single word equal to width, and a row that wraps three times. Use list joins rather than string concatenation. That covers nearly every failure mode this problem has.