Reported February 2023
ZipRecruiterstring

Format a Centered Paragraph in a Star Frame

Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live ZipRecruiter OA. Under 2s to a working solution.
Founder's read

The mistake that sinks a first attempt on this ZipRecruiter OA, reported in February 2023, is treating the sentences as separate paragraphs instead of one joined stream. This is a string simulation problem: join everything with one space, split on whitespace, greedily pack words into lines, center each line, then wrap it in stars. Nothing is hard on its own. The points get lost in the details, like odd padding and the empty-input row. If you blank on the exact rules during the live OA, StealthCoder is the safety net that reads the problem and hands you a working solution.

The problem

Join sentences with one space, split on whitespace, and greedily pack words into content lines of at most width characters using one space between words. Every word fits within width.
Return a frame whose top and bottom are width + 2 stars. Each content row has side stars and exactly width interior characters. Center a line with spaces; when the unused count is odd, put the extra space on the right. If there are no words, emit one empty centered content row.

Function
frameParagraph(sentences: String[], width: int) → String[]

Examples
Example 1
sentences = ["hello world from zip"]
width = 10
return = ["************","* hello *","*world from*","* zip *","************"]
Words are greedily packed and each line centered.
Example 2
sentences = ["abc"]
width = 6
return = ["********","* abc *","********"]
The extra odd space is placed on the right.

Constraints
1 <= width <= 1000
The total sentence length is at most 100000.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is that there's no trick, only discipline. Join the sentences with a single space, then split on any whitespace so empty strings and double spaces vanish. Greedy packing: keep a current line, and if current length + 1 + word length exceeds width, flush the line and start a new one. Centering: pad = width - len(line), left = pad / 2 (integer division), right = pad - left. That puts the extra space on the right when pad is odd. The common pitfalls are off-by-one in the packing check, putting the odd space on the left, and forgetting that zero words still yields one content row of width spaces. Build with a list and join once, since total input can hit 100000 characters. Top and bottom borders are width + 2 stars. If your edge cases wobble live, StealthCoder is the hedge that cross-checks them against the spec on screen.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Format a Centered Paragraph in a Star Frame 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. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as text justification. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass ZipRecruiter's OA.

ZipRecruiter reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Format a Centered Paragraph in a Star Frame FAQ

How hard is this ZipRecruiter question really?+

Easy on algorithm, medium on care. It's greedy packing plus padding math, all linear time. People fail on edge cases, not ideas: odd padding direction, the empty-words row, and whitespace handling. Write it cleanly and trace both examples by hand before submitting.

What's the trick to the centering?+

Compute pad = width - line length, then left = pad // 2 and right = pad - left. When pad is odd, right gets the extra space, which matches the spec. Don't use a built-in center function without checking which side it favors, because defaults vary by language.

What happens when there are no words?+

You still return a frame with one content row. That row is a star, width spaces, and a star, between two borders of width + 2 stars. Test this case first, since an empty or all-whitespace input is exactly where a loop-based solution quietly returns nothing.

How should I split the words?+

Join sentences with one space, then split on whitespace so runs of spaces and empty entries disappear. Most languages have a split that does this directly. Don't split on a single space character, or you'll get empty tokens that break your length checks.

How do I prepare for this in 48 hours?+

Write the function once from scratch and run both examples. Then add tests for width equal to a word length, an odd pad, and empty input. Practice the greedy flush condition until it's automatic. That covers nearly every way this problem breaks.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with ZipRecruiter.

OA at ZipRecruiter?
Invisible during screen share
Get it