← SoFi Interview Insights

SoFi·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jun 2026Remote

Summary

SoFi coding round, got hit with the text justification problem. Not the hardest thing in the world but the edge cases will absolutely trip you up if you're not careful.

Questions Asked (1)

Q1

Given an array of words and a maximum line width, format the text so each line is exactly that width with spaces distributed as evenly as possible between words. The last line should be left-justified.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The greedy packing part is fine, you just keep adding words until the next one doesn't fit.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem constraints and edge cases first, then outline a greedy line-by-line approach: pack as many words as fit, distribute spaces evenly (with extra spaces on the left), and handle the last line separately. Walk through a small example to validate the logic, then discuss complexity and potential optimizations.

Pro tip: Mention that you'd write helper functions for space distribution and line justification to keep the code clean and testable, and explicitly handle edge cases like a single word per line and the last line.

1. Clarify requirements and edge cases

Ask about input constraints (e.g., word length vs. maxWidth), whether words can be longer than maxWidth, and how to handle the last line. Confirm that spaces must be distributed as evenly as possible, with extra spaces going to the leftmost gaps.

2. Design the greedy line-packing algorithm

Iterate through words, adding them to the current line as long as the total length (including at least one space between words) does not exceed maxWidth. Once the next word doesn't fit, finalize the current line.

3. Implement space distribution for justified lines

For non-last lines, compute the total spaces needed and distribute them as evenly as possible. If spaces don't divide evenly, assign the extra spaces to the leftmost gaps. Handle the case of a single word by left-justifying it.

4. Handle the last line and edge cases

Left-justify the last line by joining words with a single space and padding the end with spaces to reach maxWidth. Also handle cases where a word is longer than maxWidth (though typically not allowed) and empty input.

5. Analyze complexity and test

State that the time complexity is O(n) where n is the total number of characters, as each word is processed once. Walk through a test case to verify correctness, and mention potential optimizations or trade-offs.

Key Points to Mention

  • Greedy approach: pack as many words as possible per line without exceeding maxWidth.
  • Space distribution: calculate total spaces needed, divide evenly, and distribute extra spaces from left to right.
  • Last line handling: left-justify with a single space between words and pad with trailing spaces.
  • Edge cases: single word per line, word longer than maxWidth, empty input, and lines with no extra spaces.
  • Time and space complexity: O(n) time and O(n) space for the output.
  • Code organization: use helper functions for clarity and testability.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.