68. Text Justification
hardAsked at AirbnbGiven words and maxWidth, format text so each line is exactly maxWidth chars with full justification (even space distribution). Airbnb asks this to test careful index bookkeeping and the last-line + single-word edge cases.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Airbnb loops.
- Glassdoor (2026-Q1)— Airbnb onsite reports consistently list Text Justification as a signature implementation hard.
- Blind (2025-12)— Recurring in Airbnb new-grad and L4 interview reports — Airbnb is famous for it.
Problem
Given an array of strings words and a width maxWidth, format the text such that each line has exactly maxWidth characters and is fully (left and right) justified. You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ' ' when necessary so that each line has exactly maxWidth characters. Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line does not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right. For the last line of text, it should be left-justified, and no extra space is inserted between words. Each word's length is guaranteed to be greater than 0 and not exceed maxWidth.
Constraints
1 <= words.length <= 3001 <= words[i].length <= 20words[i] consists of only English letters and symbols.1 <= maxWidth <= 100words[i].length <= maxWidth
Examples
Example 1
words = ["This", "is", "an", "example", "of", "text", "justification."], maxWidth = 16["This is an","example of text","justification. "]Example 2
words = ["What","must","be","acknowledgment","shall","be"], maxWidth = 16["What must be","acknowledgment ","shall be "]Approaches
1. Greedy line packing + per-line justify (optimal)
Greedily pack words into a line while their combined length + minimum 1 space between fits. Justify by distributing extra spaces left-biased. Last line is left-justified.
- Time
- O(total chars)
- Space
- O(total chars)
function fullJustify(words, maxWidth) {
const result = [];
let i = 0;
while (i < words.length) {
let j = i, lineLen = words[i].length;
while (j + 1 < words.length && lineLen + 1 + words[j + 1].length <= maxWidth) {
j++;
lineLen += 1 + words[j].length;
}
const numWords = j - i + 1;
const totalChars = words.slice(i, j + 1).reduce((s, w) => s + w.length, 0);
const spaces = maxWidth - totalChars;
let line;
if (j === words.length - 1 || numWords === 1) {
// last line or single-word line: left justify
line = words.slice(i, j + 1).join(' ');
line = line + ' '.repeat(maxWidth - line.length);
} else {
const gaps = numWords - 1;
const base = Math.floor(spaces / gaps);
const extra = spaces % gaps;
let s = '';
for (let k = i; k <= j; k++) {
s += words[k];
if (k < j) {
s += ' '.repeat(base + (k - i < extra ? 1 : 0));
}
}
line = s;
}
result.push(line);
i = j + 1;
}
return result;
}Tradeoff: Direct simulation. The only tricky bookkeeping is the left-bias rule (k - i < extra) and the two left-justify cases (last line, single-word line).
Airbnb-specific tips
Airbnb's bar on this is handling the edge cases out loud BEFORE coding. Say: 'Two cases force left-justify — the last line, and any line with a single word. Otherwise, distribute extra spaces left-biased: the first (spaces mod gaps) gaps get one extra.' Then code. Diving into the loop without naming the cases is where most candidates trip.
Common mistakes
- Right-biased space distribution (problem requires left bias).
- Forgetting that a single-word non-last line is also left-justified (it has no gaps to distribute over).
- Off-by-one on the line-packing condition — must include the +1 for the space between words.
Follow-up questions
An interviewer at Airbnb may pivot to one of these next:
- What if the text were right-to-left (Arabic, Hebrew)?
- What if you needed to support words longer than maxWidth (with hyphenation)?
- Implement word-wrap in a WYSIWYG editor — same idea with rendering.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why are the last line and single-word line left-justified?
The problem explicitly defines it that way — convention in book typesetting. Last lines avoid awkward giant spaces; single-word lines have no gaps to distribute.
Why left-bias on the extra space distribution?
Problem requirement. It's a convention in many typesetting systems. The math: extra = spaces mod gaps; the first 'extra' gaps each get one additional space.
Free learning resources
Curated free links for this problem.
Practice these live with InterviewChamp.AI
Drill Text Justification and other Airbnb interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →