96. Text Justification
hardAsked at SnowflakeJustify text to a fixed width, distributing spaces evenly. Snowflake asks this to test careful greedy packing with edge-case spacing — relevant to formatted CSV unload and result-set pretty-printing.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Snowflake loops.
- Glassdoor (2025-Q4)— Snowflake unload-team uses this in onsites.
- LeetCode Discuss (2025-09)— Reported at Snowflake SDE-II screens.
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. Pad extra spaces ' ' when necessary so that each line has exactly maxWidth characters. Extra spaces between words should be distributed as evenly as possible. The last line of text and lines with a single word should be left-justified.
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. Pack greedily, distribute spaces
Greedy packing: keep adding words while total + spaces <= maxWidth. Then distribute extra spaces left-biased.
- Time
- O(total chars)
- Space
- O(total chars)
function fullJustify(words, maxWidth) {
const result = [];
let i = 0;
while (i < words.length) {
let j = i, len = words[i].length;
while (j + 1 < words.length && len + 1 + words[j+1].length <= maxWidth) {
len += 1 + words[++j].length;
}
const gaps = j - i;
let line;
if (j === words.length - 1 || gaps === 0) {
// Last line or single word: left-justify
line = words.slice(i, j + 1).join(' ');
line += ' '.repeat(maxWidth - line.length);
} else {
const totalSpaces = maxWidth - len + gaps;
const base = Math.floor(totalSpaces / gaps);
const extra = totalSpaces % gaps;
line = '';
for (let k = i; k <= j; k++) {
line += words[k];
if (k < j) {
const sp = base + (k - i < extra ? 1 : 0);
line += ' '.repeat(sp);
}
}
}
result.push(line);
i = j + 1;
}
return result;
}Tradeoff: Greedy with care on edge cases. The last-line and single-word rules are easy to miss.
2. Same as above (only one viable approach)
There isn't a meaningfully different optimal — this is the standard solution.
- Time
- O(total chars)
- Space
- O(total chars)
// see aboveTradeoff: Linear in total output size.
Snowflake-specific tips
Snowflake interviewers grade this on edge-case rigor: last-line left-justified, single-word line left-justified, uneven space distribution biased left. Bonus signal: pivot to formatted unload — when Snowflake exports CSV with column padding, the executor follows similar packing-and-distributing logic.
Common mistakes
- Treating the last line like a normal line (must be left-justified).
- Distributing extra spaces evenly even when one word per line (just pad on the right).
- Off-by-one with the +1 for the trailing space between words.
Follow-up questions
An interviewer at Snowflake may pivot to one of these next:
- Right-justify variant.
- Center-justify variant.
- How does Snowflake's CSV unload format fixed-width columns?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why left-bias the extra spaces?
The spec mandates it: when spaces don't divide evenly, the leftmost gaps get the extras. This is a typographic convention.
Why a special case for the last line?
Books and newspapers conventionally leave the last line ragged-right — left-justified with single spaces. The spec follows that convention.
Practice these live with InterviewChamp.AI
Drill Text Justification and other Snowflake interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →