FastPrepWord Wrap with Even Justification
Problems/Word Wrap with Even Justification

Word Wrap with Even Justification

Sentry logoSentry● HardINTERNNEW GRADOAPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A firsthand Sentry intern online assessment report dated August 17, 2023 includes five screenshots of this full word-wrapping challenge. It covers exactly how spaces are spread, how a single word is centered, and the hyphen rules. Our version is about 98% the same as the reported question.

Format a sequence of words into fixed-width lines. A word is any run of non-whitespace characters, so it may contain letters, digits, punctuation and other symbols. 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 = 6return = ["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 = 12return = ["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 = 8return = [" auto-  ","complete","is    my"," go-to  "]

The long hyphenated word splits after auto-, while whitespace around the hyphen in go - to is ignored.

Example 4

text = "Ship v2.0 now!"width = 9return = ["Ship v2.0","  now!   "]

Words may contain digits and punctuation. Ship v2.0 fills the first line exactly, and now! is centered with two spaces before it and three after.

Constraints

  • 1 <= text.length <= 100000.
  • 1 <= width <= 200.
  • Whitespace in text is a space, tab, newline or carriage return; every other character is a printable ASCII character such as a letter, digit, punctuation mark, symbol or hyphen.
  • After normalizing whitespace around hyphens, every ordinary word and every segment between allowed hyphen splits fits within width.

More Sentry interview questions

  • Wildcard Bad-Word FilterOA · Seen Aug 2022
  • Find All Word Occurrences in TextPHONE SCREEN · Seen Mar 2023
  • Five-Minute Hit CounterPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2022
  • Minimum Meeting RoomsPHONE SCREEN · Seen Feb 2022

Similar questions at other companies

  • Greedy and Beam Sentence DecodingMicrosoft · PHONE SCREEN · ONSITE INTERVIEWMicrosoft interview questions
  • Text JustificationUber · PHONE SCREENUber interview questions
  • Max Number of OperationsSalesforce · OASalesforce interview questions
  • Robot Assembly Line ConflictsGoogle · OAGoogle interview questions
  • Minimum Steps on a Circular Letter DialIBM · OAIBM interview questions
  • Format a Centered Newspaper PageVisa · OAVisa interview questions
  • Choose the Next Hidden-Word GuessCharacter.AI · PHONE SCREEN
  • Choose the Next Hidden-Word GuessFlexport · ONSITE INTERVIEWFlexport interview questions
  • URL Segment Compression Part 3 — Global Token BudgetStripe · ONSITE INTERVIEWStripe interview questions
  • Lexicographically Largest IdentifierAmazon · OA

Interview reports that mention this question

  • Sentry Software Engineer Interview Questions: 18 Experiences
  • Sentry SWE Intern OA and Technical Interview: 16 Experiences
  • Sentry SWE New Grad OA and Technical Screen: 12 Experiences

Sentry interview experiences

  • Sentry Experienced First Technical Interview: 8 Experiences
  • Sentry SWE Intern First Technical Interview: 10 Experiences
  • Sentry SWE New Grad First Technical Interview: 4 Experiences
All Sentry interview questionsSee Sentry hiring insights
Saved
class Solution {
    public String[] justifyWords(String text, int width) {
        // Write your code here.
    }
}
Checking account…