Skip to content
Katabench
Try free

Text Justification

Hard About 40 min Free, no account needed

Greedily pack words into fully justified lines, spaces and all.

The brief

Given words and a line maxWidth, format the text fully justified:

  • pack greedily: as many words per line as fit,
  • distribute spaces evenly between words; when they do not divide evenly, the leftmost gaps get the extra space,
  • the last line (and any line with a single word) is left-justified: words separated by one space, padded with spaces on the right,
  • every output line is exactly maxWidth characters.

The hidden trap

The space arithmetic at the edges: a line with one word, the uneven-gap distribution, and the left-justified last line. The hidden tests target each.

Starter code

C#
using System;

public class Solution
{
    // TODO: return fully justified lines of exactly maxWidth characters.
    public string[] FullJustify(string[] words, int maxWidth)
    {
        throw new NotImplementedException();
    }
}

How it is graded

Visible examples help you iterate. Hidden tests then cover edge cases and larger inputs, and every test reports server-measured time and allocated bytes.

C# coding challenges →

What it practices

String processing
Parse, scan and match text character by character with correct edge cases.
Greedy algorithms
Make the locally best choice when an exchange argument proves it stays globally optimal.

Related katas