Text Justification
Updated
Problem statement
Given an array of words and a width maxWidth, format the text so that every line has exactly maxWidth characters and is fully justified.
- Pack words greedily: put as many words on a line as fit when separated by single spaces.
- Spread the extra spaces between the words of a line as evenly as possible. If they do not divide evenly, the gaps on the left get one more space than the gaps on the right.
- A line with only one word is left-justified: the word, then spaces up to
maxWidth. - The last line is left-justified: words separated by single spaces, then spaces up to
maxWidth.
Return the lines in order.
Function
fullJustify(words: String[], maxWidth: int) → List<String>Examples
Example 1
words = ["This","is","an","example","of","text","justification."]maxWidth = 16return = ["This is an","example of text","justification. "]- The first line holds
This is an(8 letters), so 8 spaces fill 2 gaps: 4 each. - The second line holds
example of text(13 letters): 3 spaces over 2 gaps, so the left gap gets 2 and the right gap 1. - The last line is left-justified and padded on the right.
Example 2
words = ["What","must","be","acknowledgment","shall","be"]maxWidth = 16return = ["What must be","acknowledgment ","shall be "]acknowledgmentis alone on its line (it does not fit with the next word), so it is left-justified and padded:"acknowledgment ".- The last line
shall beis left-justified even though it has two words.
Example 3
words = ["a"]maxWidth = 1return = ["a"]- One word that exactly fills the width.
Constraints
1 <= words.length <= 3001 <= words[i].length <= 20words[i]consists of printable non-space ASCII characters (letters, digits and symbols).1 <= maxWidth <= 100words[i].length <= maxWidth
More Airbnb interview questions
- Read Across Sound TracksPHONE SCREEN · Seen Nov 2026
- Decode and Store StringsPHONE SCREEN · Seen Oct 2026
- Print Sentences as TablePHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Detonate Bombs with Chain ReactionsPHONE SCREEN · Seen Aug 2026
- Smallest Common RegionPHONE SCREEN · Seen Aug 2026
- Longest Chain BookingPHONE SCREEN · Seen Aug 2026
- Nested List Iterator with RemovePHONE SCREEN · Seen Jul 2026
- Parse Query StringPHONE SCREEN · Seen May 2026
- Credit Accounts with Scheduled TransfersOA · Seen Oct 2026
- Parcel Attribute Store with TTLOA · Seen Oct 2026
Similar questions at other companies
- Greedy and Beam Sentence DecodingMicrosoft · PHONE SCREEN · ONSITE INTERVIEWMicrosoft interview questions
- Design In-Memory File SystemSnowflake · PHONE SCREENSnowflake interview questions
- Text JustificationUber · PHONE SCREENUber interview questions
- Parse and Query Server LogsKikoff · PHONE SCREENKikoff interview questions
- Word Wrap with Even JustificationSentry · OA · PHONE SCREENSentry interview questions
- Search and Format ContactsAdyen · PHONE SCREENAdyen interview questions
- Subtract Two Large Number StringsStackAdapt · PHONE SCREENStackAdapt interview questions
- Business Account KYC Verification — Parts 1–5Stripe · PHONE SCREENStripe interview questions
- Stateful Log Transform and SearchRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions