Reported August 2023
Sentrystring

Word Wrap with Even Justification

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

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

Sentry's August 2023 OA hands you a text justification problem with a few nasty twists. Underneath, it's string parsing plus greedy line packing, then a justify pass. The hyphen rules are what make it feel hard. Whitespace around a hyphen vanishes, and a long hyphenated word splits at the rightmost hyphen that fits. Nothing here needs a clever algorithm. It needs careful ordering of steps and clean edge-case handling. If you blank on the details during the live assessment, StealthCoder is the invisible safety net that reads the problem and gives you a working structure.

The problem

Format a sequence of words into fixed-width lines. Any run of whitespace separates words, except that whitespace immediately around a hyphen is ignored, so go - to becomes go-to.
Pack words greedily using one space while deciding what fits. Every returned line must then have exactly width characters. For a line with multiple words, distribute as many spaces as possible equally between every adjacent pair and append any remainder to the end. Center a one-word line, placing an unmatched extra space at the end.
Keep a hyphenated word intact when its normalized form fits within width. When it is longer than width, repeatedly split at the rightmost hyphen whose left piece, including that hyphen, fits; treat the pieces as consecutive words.

Function
justifyWords(text: String, width: int) → String[]

Examples
Example 1
text = "a cat is an animal"
width = 6
return = ["a cat","is an","animal"]
After greedy packing, final justification gives both two-word lines six characters.
Example 2
text = "cat is an animal and so is a dog"
width = 12
return = ["cat is an ","animal and","so is a dog "]
The first line has two spaces per gap and one leftover space at the end.
Example 3
text = "auto-complete is my go - to"
width = 8
return = [" auto- ","complete","is my"," go-to "]
The long hyphenated word splits after auto-, while whitespace around the hyphen in go - to is ignored.

Constraints
1 <= text.length <= 100000.
1 <= width <= 200.
The input contains printable ASCII characters and whitespace; non-whitespace text consists of letters and hyphens.
After normalizing whitespace around hyphens, every ordinary word and every segment between allowed hyphen splits fits within width.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The problem reduces to three stages. First, normalize: collapse whitespace runs, then strip spaces around hyphens so "go - to" becomes "go-to". Second, tokenize into words, and for any word longer than width, repeatedly split at the rightmost hyphen where the left piece including the hyphen fits, then treat the pieces as separate words. Third, pack greedily assuming one space between words, then justify each line. For multi-word lines, gap = spaces / (words - 1), and the remainder goes at the END, not spread left. One-word lines get centered with the extra space at the end. The common pitfall is justifying the last line differently, like classic LeetCode. Here every line gets full width, and example 3 shows centered single words. Also watch the hyphen split: the left piece keeps its hyphen. With text up to 100000 characters, build strings with joins, not repeated concatenation. StealthCoder is your hedge if the split logic tangles.

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 Word Wrap with Even Justification 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

⏵ The honest play

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

Sentry 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.

Word Wrap with Even Justification FAQ

What's the trick in Sentry's word wrap with even justification problem?+

Separate the stages. Normalize whitespace around hyphens, split oversized words at the rightmost fitting hyphen, pack greedily with single spaces, then justify. Most bugs come from mixing these steps together instead of running them as a clean pipeline.

How is this different from LeetCode Text Justification?+

Two big differences. Every line here is padded to exactly width, including the last one, and extra spaces go at the end instead of the left gaps. Single-word lines are centered. Plus the hyphen normalization and splitting rules are entirely new.

How do I handle a hyphenated word longer than width?+

Find the rightmost hyphen where the prefix, including the hyphen, is at most width. Cut there, emit the left piece as a word, and repeat on the remainder. The constraints guarantee each final segment fits, so the loop always terminates.

Which edge cases should I test before submitting?+

Test a single-word line with an odd leftover, so the extra space lands at the end. Test spaces around hyphens like "go - to", multiple whitespace runs, and a word exactly equal to width. Also test input with a leading or trailing space.

How do I prepare for this in 48 hours?+

Write the pipeline from scratch twice: normalize, split, pack, justify. Use the three given examples as tests. Practice the gap math with divmod. Then trace example 3 by hand, since it exercises both the hyphen split and the centering rule.

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

OA at Sentry?
Invisible during screen share
Get it