PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/Coding & Algorithms/Faire

Format text into fixed-width justified lines

Last updated: Mar 29, 2026

Quick Overview

This question evaluates string manipulation, greedy packing, and precise space-distribution skills, assessing competency in algorithmic implementation and careful handling of formatting edge cases.

  • hard
  • Faire
  • Coding & Algorithms
  • Software Engineer

Format text into fixed-width justified lines

Company: Faire

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

You are given an array of strings `words` (each string is a word with no spaces) and an integer `maxWidth`. Return a list of lines (strings) that formats the words into lines of **exactly** `maxWidth` characters each. ## Rules 1. **Greedy packing:** Build lines from left to right using as many words as possible per line (without exceeding `maxWidth`). 2. **Do not split words:** A word must appear entirely on one line. 3. **Space distribution (non-last lines):** - Suppose a line contains `k` words. - There are `k-1` mandatory gaps *between* adjacent words; each such gap must have **at least 1 space**. - After placing those mandatory spaces, distribute any **remaining spaces** as evenly as possible across **all `k+1` gaps**: - the gap **before the first word** (leading spaces), - the `k-1` gaps **between words**, - the gap **after the last word** (trailing spaces). - If the spaces cannot be distributed evenly, assign the extra one-by-one starting from the **leftmost gap** (leading gap first, then left-to-right). - This means a line **may start and/or end with spaces**. 4. **Last line:** Left-justify it: use a single space between words and put all remaining spaces at the **end** of the line. ## Input / Output - **Input:** `words: string[]`, `maxWidth: int` - **Output:** `string[]` where each string has length exactly `maxWidth`. ## Example If `words = ["a","b","c"]` and `maxWidth = 5`: - All three words fit on one line as the last line: `"a b c"` has length 5, so output is `["a b c"]`. ## Constraints - `1 <= words.length <= 300` - `1 <= len(words[i]) <= maxWidth <= 100` - `words[i]` contains no spaces.

Quick Answer: This question evaluates string manipulation, greedy packing, and precise space-distribution skills, assessing competency in algorithmic implementation and careful handling of formatting edge cases.

Related Interview Questions

  • Print a Centered Pascal Triangle - Faire (medium)
  • Find First Haiku in Sentence - Faire (Medium)
  • Find first 5-7-5 haiku in sentence - Faire (Medium)
Faire logo
Faire
Jan 6, 2026, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
4
0
Loading...

You are given an array of strings words (each string is a word with no spaces) and an integer maxWidth.

Return a list of lines (strings) that formats the words into lines of exactly maxWidth characters each.

Rules

  1. Greedy packing: Build lines from left to right using as many words as possible per line (without exceeding maxWidth ).
  2. Do not split words: A word must appear entirely on one line.
  3. Space distribution (non-last lines):
    • Suppose a line contains k words.
    • There are k-1 mandatory gaps between adjacent words; each such gap must have at least 1 space .
    • After placing those mandatory spaces, distribute any remaining spaces as evenly as possible across all k+1 gaps :
      • the gap before the first word (leading spaces),
      • the k-1 gaps between words ,
      • the gap after the last word (trailing spaces).
    • If the spaces cannot be distributed evenly, assign the extra one-by-one starting from the leftmost gap (leading gap first, then left-to-right).
    • This means a line may start and/or end with spaces .
  4. Last line: Left-justify it: use a single space between words and put all remaining spaces at the end of the line.

Input / Output

  • Input: words: string[] , maxWidth: int
  • Output: string[] where each string has length exactly maxWidth .

Example

If words = ["a","b","c"] and maxWidth = 5:

  • All three words fit on one line as the last line: "a b c" has length 5, so output is ["a b c"] .

Constraints

  • 1 <= words.length <= 300
  • 1 <= len(words[i]) <= maxWidth <= 100
  • words[i] contains no spaces.

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Faire•More Software Engineer•Faire Software Engineer•Faire Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.