Reported February 2021
Bloombergsimulation

Text Justification

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

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

The data structure behind Text Justification is nothing fancy. It's an array of words and a string builder, and the whole problem is careful index math. Bloomberg candidates reported this one in February 2021, and it's a greedy simulation with a lot of edge cases. If you've got an OA invite coming, expect to lose points on spacing rules, not on the idea. You'll pack words per line, then pad. Easy to describe, easy to botch under a timer. StealthCoder runs invisibly during the live OA, so if your spacing logic falls apart, you have a safety net.

The problem

Given an array of non-empty strings words and an integer maxWidth, format the words into lines of exactly maxWidth characters.
Greedily place as many words as possible on each line. For every non-final line containing multiple words, distribute spaces as evenly as possible; when they do not divide evenly, gaps farther left receive one extra space. A non-final line containing one word is padded on the right.
The final line is left-justified with one space between adjacent words and trailing spaces added to reach maxWidth. Return the formatted lines.

Function
fullJustify(words: String[], maxWidth: int) → List<String>

Examples
Example 1
words = ["This", "is", "an", "example", "of", "text", "justification."]
maxWidth = 16
return = ["This is an", "example of text", "justification. "]
The greedy grouping produces three lines of exactly 16 characters. Spaces on non-final lines are distributed evenly, while the final line is padded on the right.
Example 2
words = ["What","must","be","acknowledgment","shall","be"]
maxWidth = 16
return = ["What must be", "acknowledgment ", "shall be "]
The one-word middle line is padded on the right. The final line uses one space between its words and trailing spaces to reach width 16.
Example 3
words = ["Science","is","what","we","understand","well","enough","to","explain","to","a","computer.","Art","is","everything","else","we","do"]
maxWidth = 20
return = ["Science is what we","understand well","enough to explain to","a computer. Art is","everything else we","do "]
Each line has exactly 20 characters. Extra spaces on non-final lines are assigned to the leftmost gaps, and the final line is right-padded.

Constraints
1 <= words.length <= 300
1 <= words[i].length <= 20
1 <= maxWidth <= 100
words[i].length <= maxWidth
Every word contains only English letters and symbols.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is two phases per line. First, greedily scan words while the sum of word lengths plus minimum single spaces fits in maxWidth. Second, build the line. For a non-final line with multiple words, compute total spaces = maxWidth minus letter count, gaps = words - 1, then each gap gets total / gaps and the first total % gaps gaps get one extra. That's the left-bias rule. Pitfalls: a one-word line has zero gaps, so dividing by zero crashes you. Handle it by left-justifying and padding right. The last line also left-justifies with single spaces, then pads right. Don't forget to flush the final line. Use a list and join, not repeated concatenation. Runtime is linear in total characters. If you freeze on the off-by-one in the greedy check, StealthCoder is the hedge on the live OA, reading the problem and giving you working code.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Text 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

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 Bloomberg's OA.

Bloomberg reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Text Justification FAQ

How hard is Text Justification really?+

The algorithm is easy but the implementation is fiddly. It's a hard-tagged problem because of edge cases, not because of theory. If you can write clean greedy packing and a spacing formula, you'll pass. Most failures come from single-word lines and the last line.

What's the trick to distributing spaces?+

Use integer division and remainder. Spaces per gap = totalSpaces / gaps, and the first totalSpaces % gaps gaps get one extra. That naturally puts the extra spaces on the left, which is exactly what the problem demands.

What edge cases break most solutions?+

A line with a single word, since gaps is zero and you'd divide by zero. The final line, which needs single spaces and right padding. And forgetting to flush the leftover words after the loop ends. Test all three before submitting.

Was this reported recently at Bloomberg?+

It was reported by candidates in February 2021. Text Justification is a long-standing string simulation question, so the pattern is worth knowing, but don't assume the exact problem will show up again. Practice the greedy pack plus pad skeleton.

How do I prepare in 48 hours?+

Write it from scratch twice. First pass: the greedy line grouping. Second pass: the spacing builder with the remainder rule. Then run the three examples by hand, especially the one-word middle line. Focus on index bounds, not on cleverness.

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

OA at Bloomberg?
Invisible during screen share
Get it