68. Text Justification
hardAsked at GoogleGiven an array of words and a max line width, format the text so each line is fully justified. Google asks this to see whether you can manage the edge cases (last line, single-word lines, uneven space distribution) without losing track.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Google loops.
- Glassdoor (2026-Q1)— Google L4/L5 onsite reports cite this as the 'implementation/edge-case' round.
- Blind (2025-08)— Recurring Google interview problem flagged as deceptively hard.
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.
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. "]Approaches
1. Greedy line packing + per-line justify (only practical approach)
Greedily pack words into lines (up to maxWidth). For each completed line, distribute extra spaces evenly (with extras going to the left gaps). The last line is left-justified.
- Time
- O(n)
- Space
- O(n)
function fullJustify(words, maxWidth) {
const result = [];
let i = 0;
while (i < words.length) {
let lineLen = words[i].length;
let j = i + 1;
while (j < words.length && lineLen + 1 + words[j].length <= maxWidth) {
lineLen += 1 + words[j].length;
j++;
}
const wordCount = j - i;
const isLast = j === words.length;
let line;
if (wordCount === 1 || isLast) {
line = words.slice(i, j).join(' ');
line += ' '.repeat(maxWidth - line.length);
} else {
const totalChars = words.slice(i, j).reduce((s, w) => s + w.length, 0);
const totalSpaces = maxWidth - totalChars;
const gaps = wordCount - 1;
const base = Math.floor(totalSpaces / gaps);
const extra = totalSpaces % gaps;
line = '';
for (let k = i; k < j; k++) {
line += words[k];
if (k < j - 1) {
const spaces = base + (k - i < extra ? 1 : 0);
line += ' '.repeat(spaces);
}
}
}
result.push(line);
i = j;
}
return result;
}Tradeoff: There's only one reasonable approach — the difficulty is entirely in case management. The 'left-gaps get extras' rule is the bit candidates often miss.
Google-specific tips
Google interviewers grade this purely on edge-case discipline. The three cases — (1) line with one word, (2) last line, (3) normal multi-word line — each have different padding rules. Articulate the three cases before coding. Don't try to handle them in one branch; the code will be unreadable.
Common mistakes
- Treating single-word lines as multi-word (you'd divide by zero on the gap count).
- Treating the last line as a normal line (it must be left-justified, not fully justified).
- Distributing extra spaces evenly instead of left-bias (extras go to the LEFT gaps first).
Follow-up questions
An interviewer at Google may pivot to one of these next:
- What if you needed center-justification on the last line?
- What if you had to handle hyphenation across line breaks?
- What if maxWidth could change per line (e.g., a magazine layout)?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why is this rated hard if there's no clever algorithm?
The difficulty is correctness-under-edge-cases, not algorithmic depth. Most candidates miss either the last-line rule or the left-bias on uneven space distribution.
How long should this take in an interview?
Roughly 30-40 minutes. If you're not done by then, you're either over-engineering or you forgot to enumerate the three cases upfront.
Practice these live with InterviewChamp.AI
Drill Text Justification and other Google interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →