Fully Justify Text to a Fixed Width

Quick Overview

Practice canonical text justification with greedy line packing, even space distribution, left-biased remainders, and final-line rules.

Fully Justify Text to a Fixed Width

Company: Anyscale

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

# Fully Justify Text to a Fixed Width Format a sequence of words into lines of exactly max_width characters. Pack each line greedily with as many words as fit. Fully justify every nonfinal line by distributing spaces as evenly as possible; when they do not divide evenly, earlier gaps receive the extra spaces. The final line is left-justified with single spaces and trailing padding. ### Function Signature ```text full_justify(words: list[str], max_width: int) -> list[str] ``` ### Valid Input Domain Words is nonempty, every word is nonempty ASCII text without spaces, and no word is longer than max_width. ### Exact Output Semantics Return lines in input word order. Every returned string has exactly max_width characters. A nonfinal one-word line is padded on the right. The final line uses one space between words and pads the right. These rules produce one canonical output. ### Constraints - 1 <= words.length <= 10,000. - 1 <= words[i].length <= max_width <= 1,000. - Packing a word considers the minimum one separating space from the preceding word. ### Public Examples #### Example 1 Input: words = ["This", "is", "an", "example", "of", "text", "justification."], max_width = 16 Output: ["This is an", "example of text", "justification. "] The first two lines are fully justified; the final line is left-justified. #### Example 2 Input: words = ["What", "must", "be", "acknowledgment", "shall", "be"], max_width = 16 Output: ["What must be", "acknowledgment ", "shall be "] The middle line has one word, and the last line uses single internal spacing. ### Hints - Decide line membership before calculating its spacing. - For a fully justified line, divide total required spaces across the number of gaps and assign any remainder from left to right.

Overview: Practice canonical text justification with greedy line packing, even space distribution, left-biased remainders, and final-line rules.

|Home/Coding & Algorithms/Anyscale
Anyscale logo
Anyscale
Aug 23, 2026
easySoftware EngineerTechnical ScreenCoding & Algorithms
0
0

Fully Justify Text to a Fixed Width

Format a sequence of words into lines of exactly max_width characters. Pack each line greedily with as many words as fit. Fully justify every nonfinal line by distributing spaces as evenly as possible; when they do not divide evenly, earlier gaps receive the extra spaces. The final line is left-justified with single spaces and trailing padding.

Function Signature

full_justify(words: list[str], max_width: int) -> list[str]

Valid Input Domain

Words is nonempty, every word is nonempty ASCII text without spaces, and no word is longer than max_width.

Exact Output Semantics

Return lines in input word order. Every returned string has exactly max_width characters. A nonfinal one-word line is padded on the right. The final line uses one space between words and pads the right. These rules produce one canonical output.

Constraints

  • 1 <= words.length <= 10,000.
  • 1 <= words[i].length <= max_width <= 1,000.
  • Packing a word considers the minimum one separating space from the preceding word.

Public Examples

Example 1

Input: words = ["This", "is", "an", "example", "of", "text", "justification."], max_width = 16

Output: ["This is an", "example of text", "justification. "]

The first two lines are fully justified; the final line is left-justified.

Example 2

Input: words = ["What", "must", "be", "acknowledgment", "shall", "be"], max_width = 16

Output: ["What must be", "acknowledgment ", "shall be "]

The middle line has one word, and the last line uses single internal spacing.

Hints

  • Decide line membership before calculating its spacing.
  • For a fully justified line, divide total required spaces across the number of gaps and assign any remainder from left to right.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...