PracHub
QuestionsPremiumLearningGuidesInterview PrepCoaches
|Home/Coding & Algorithms/SoFi

Format words into wrapped/justified lines

Last updated: Apr 19, 2026

Quick Overview

This question evaluates practical programming skills in string processing, greedy packing, and careful handling of spacing and edge cases, and falls under the Coding & Algorithms domain.

  • medium
  • SoFi
  • Coding & Algorithms
  • Software Engineer

Format words into wrapped/justified lines

Company: SoFi

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given a list of words (strings with no spaces) and an integer `maxWidth`. Implement text formatting in two variants: ## Variant A — Basic word wrap (no padding) Return lines by packing words **greedily** from left to right: - Each line contains as many words as possible. - Words in a line are separated by **a single space**. - **Do not** pad the end of the line with extra spaces. ## Variant B — Full justification (fixed width) Return lines of **exactly** `maxWidth` characters: - Pack words greedily as in Variant A. - For every line **except the last**: - Distribute spaces between words so that the line length is exactly `maxWidth`. - If the spaces do not divide evenly, the **leftmost gaps** get more spaces. - If a line has only **one** word, left-justify it and pad trailing spaces to reach `maxWidth`. - For the **last** line: - Left-justify: words separated by a single space, then pad trailing spaces to reach `maxWidth`. ### Input - `words: List[str]` - `maxWidth: int` ### Output - `List[str]` lines formatted according to the chosen variant. ### Constraints (typical) - `1 <= len(words) <= 10^4` - `1 <= len(words[i]) <= maxWidth` - `1 <= maxWidth <= 100`

Quick Answer: This question evaluates practical programming skills in string processing, greedy packing, and careful handling of spacing and edge cases, and falls under the Coding & Algorithms domain.

Related Interview Questions

  • Find Smallest Common Row Value - SoFi (easy)
  • Find the second most frequent tag - SoFi (medium)
  • Implement a multithreaded task executor with semaphores - SoFi (medium)
  • Implement chance of a personal best - SoFi (hard)
  • Solve Time-Window and Binary Swap Problems - SoFi (easy)
SoFi logo
SoFi
Feb 12, 2026, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
19
0
Coding Console
Loading...

You are given a list of words (strings with no spaces) and an integer maxWidth.

Implement text formatting in two variants:

Variant A — Basic word wrap (no padding)

Return lines by packing words greedily from left to right:

  • Each line contains as many words as possible.
  • Words in a line are separated by a single space .
  • Do not pad the end of the line with extra spaces.

Variant B — Full justification (fixed width)

Return lines of exactly maxWidth characters:

  • Pack words greedily as in Variant A.
  • For every line except the last :
    • Distribute spaces between words so that the line length is exactly maxWidth .
    • If the spaces do not divide evenly, the leftmost gaps get more spaces.
    • If a line has only one word, left-justify it and pad trailing spaces to reach maxWidth .
  • For the last line:
    • Left-justify: words separated by a single space, then pad trailing spaces to reach maxWidth .

Input

  • words: List[str]
  • maxWidth: int

Output

  • List[str] lines formatted according to the chosen variant.

Constraints (typical)

  • 1 <= len(words) <= 10^4
  • 1 <= len(words[i]) <= maxWidth
  • 1 <= maxWidth <= 100

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More SoFi•More Software Engineer•SoFi Software Engineer•SoFi 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.