Skip to main content

68. Text Justification

hardAsked at LinkedIn

Given an array of words and a max line width, fully justify each line (with the last line left-aligned). LinkedIn asks this for the hard slot because it's the canonical 'simulate carefully' problem — no clever algorithm, just precise spacing distribution.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in LinkedIn loops.

  • Glassdoor (2026-Q1)LinkedIn senior IC onsite reports cite Text Justification as one of their signature hard problems.
  • Blind (2025-12)LinkedIn SWE writeups consistently rank Text Justification among the top 5 LinkedIn-tagged problems on public aggregators.

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 <= 300
  • 1 <= words[i].length <= 20
  • words[i] consists of only English letters and symbols.
  • 1 <= maxWidth <= 100
  • words[i].length <= maxWidth

Examples

Example 1

Input
words = ["This", "is", "an", "example", "of", "text", "justification."], maxWidth = 16
Output
["This    is    an","example  of text","justification.  "]

Example 2

Input
words = ["What","must","be","acknowledgment","shall","be"], maxWidth = 16
Output
["What   must   be","acknowledgment  ","shall be        "]

Explanation: Last line and any line containing a single word are left-justified.

Approaches

1. Greedy line-packing + per-line justification

Greedily pack words into lines (each line gets as many words as fit). For each line except the last, distribute spaces evenly among gaps with left-side gaps getting extra. For the last line and single-word lines, left-align then right-pad with spaces.

Time
O(n) total chars output (n is total chars across all words)
Space
O(n) for output
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) {
      lineLen += 1 + words[++j].length;
    }
    const gaps = j - i;
    const isLast = j === words.length - 1;
    let line;
    if (gaps === 0 || isLast) {
      line = words.slice(i, j + 1).join(' ');
      line += ' '.repeat(maxWidth - line.length);
    } else {
      const totalSpaces = maxWidth - (lineLen - gaps);
      const baseSpaces = Math.floor(totalSpaces / gaps);
      const extra = totalSpaces % gaps;
      line = '';
      for (let k = i; k < j; k++) {
        line += words[k];
        line += ' '.repeat(baseSpaces + (k - i < extra ? 1 : 0));
      }
      line += words[j];
    }
    result.push(line);
    i = j + 1;
  }
  return result;
}

Tradeoff: O(n) total work. The only 'algorithm' is the line-fit loop; the rest is careful arithmetic on space distribution. Watch the three cases: single-word line, last line, normal line.

LinkedIn-specific tips

LinkedIn interviewers grade three things: (1) Do you handle the 'single word on a line' case (left-align + right-pad)? (2) Do you handle the last line correctly (left-align even if it has multiple words)? (3) Do you distribute uneven spaces left-heavy? Most candidates get 1 and 2 but break on 3 — write out the case for `gaps = 3, totalSpaces = 5` (each gap gets 1, plus 2 extra on the left two gaps).

Common mistakes

  • Forgetting that single-word lines must be LEFT-justified (with trailing spaces), not center.
  • Forgetting the last-line special case — must be left-justified regardless.
  • Distributing extra spaces evenly (e.g., one extra per gap) instead of left-heavy (first `extra` gaps get one extra each).

Follow-up questions

An interviewer at LinkedIn may pivot to one of these next:

  • What if you also had to support center-justification?
  • What if words could be broken at hyphens?
  • How would you handle Unicode (variable-width glyphs)?

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why is the greedy approach correct?

Because the problem explicitly demands greedy: 'pack your words in a greedy approach.' Each line takes as many words as fit. Without greedy, the problem would be a DP min-cost-line-breaking problem (Knuth-Plass).

Why is this rated hard despite no clever algorithm?

Because the simulation has three corner cases (single-word, last-line, normal) and one arithmetic trap (left-heavy distribution). Under interview pressure most candidates miss at least one — the hard rating reflects bug density, not algorithmic depth.

Free learning resources

Curated free links for this problem.

Practice these live with InterviewChamp.AI

Drill Text Justification and other LinkedIn interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →