Justify Text by Line Count, by Fixed Width, and Across Multiple Columns

Quick Overview

A three-part text layout exercise: fit an article into a given number of lines, wrap and justify it at a fixed width, then print it in several side-by-side columns. It tests line-breaking logic, even space distribution, last-line handling and careful debugging of printed output.

Justify Text by Line Count, by Fixed Width, and Across Multiple Columns

Company: Airbnb

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Technical Screen

This phone-screen question is a text-justification exercise in three parts that build on each other. The input is the text of an article; treat it as a sequence of words separated by whitespace, keep the words in order, and never split a word across lines. Each part prints its result one line at a time. The report names the three parts but not their exact rules, so agree on the layout rules with the interviewer, using a small worked example, before coding each part. ### Clarifying Questions - Should lines be fully justified, with extra spaces spread so that both edges align, or only left-aligned? How is the last line handled? - When the extra spaces do not divide evenly among the gaps of a line, which gaps get the extra ones? - Can a single word be longer than the line width, and if so, what should happen? - Are paragraph breaks in the article kept? - Should the functions print the lines or return them, and do trailing spaces matter? ### Part 1 — A fixed number of lines Given the text and a number of lines `k`, lay the text out in `k` lines and print them. ```hint Define "best" first Agree on what makes one `k`-line layout better than another, then ask how the number of lines changes as the allowed width grows. ``` #### Clarifying Questions for this Part - Must the layout use exactly `k` lines, or at most `k`? What if the text has fewer than `k` words? - Should the lines be as even as possible in length, or in number of words? #### What This Part Should Cover - A precise objective for the layout, agreed before coding - An algorithm that meets it, with its complexity - Justifying the resulting lines, and handling `k` larger than the number of words ### Part 2 — A fixed width Given a width `W`, break the text into lines of at most `W` characters, justify them, and print them. ```hint Words first, spaces second Decide which words go on a line before deciding where its spaces go, and trace what your loop does with the words still pending when the input ends. ``` #### What This Part Should Cover - Filling lines with a length check that counts the separating spaces correctly - Distributing spaces within a line, single-word lines, and the last line - Printing every line exactly once, including the final one ### Part 3 — Multiple columns Lay the article out in several columns side by side, as on a newspaper page, and print the page row by row. ```hint Place before printing Treat the finished lines as a list and work out which line belongs in each row of each column before printing anything. ``` #### Clarifying Questions for this Part - Is the number of columns given, or the column height? What are the column width and the gap between columns? - Do lines fill the first column from top to bottom before moving to the next? - How are rows printed when the last column is shorter than the others? #### What This Part Should Cover - Reusing the fixed-width line breaking for each column's width - Mapping a line's index to a column and a row, and padding short columns - Printing row by row with the columns aligned ### What a Strong Answer Covers - Precise layout rules and a worked example agreed before coding each part - One line-breaking routine shared by all three parts - Exact widths: no off-by-one errors in counting spaces, and correct padding - Tests for edge cases: an empty text, a single word, a word exactly as long as the width - Debugging by tracing a small example, and a clear complexity analysis ### Follow-up Questions - How would you choose line breaks that minimize total raggedness (for example the sum of squared trailing spaces) instead of filling each line greedily? - How would you handle a word longer than the width? - If the page has a fixed height, how would Part 3 decide the number of columns and continue onto the next page? - How would you lay out a very long article without holding every line in memory?

Overview: A three-part text layout exercise: fit an article into a given number of lines, wrap and justify it at a fixed width, then print it in several side-by-side columns. It tests line-breaking logic, even space distribution, last-line handling and careful debugging of printed output.

Read the full Airbnb Software Engineer interview experience this question came from

Airbnb logo
Airbnb
Oct 30, 2026
mediumSoftware EngineerTechnical ScreenSoftware Engineering Fundamentals
1
0

This phone-screen question is a text-justification exercise in three parts that build on each other. The input is the text of an article; treat it as a sequence of words separated by whitespace, keep the words in order, and never split a word across lines. Each part prints its result one line at a time.

The report names the three parts but not their exact rules, so agree on the layout rules with the interviewer, using a small worked example, before coding each part.

Clarifying Questions Guidance

  • Should lines be fully justified, with extra spaces spread so that both edges align, or only left-aligned? How is the last line handled?
  • When the extra spaces do not divide evenly among the gaps of a line, which gaps get the extra ones?
  • Can a single word be longer than the line width, and if so, what should happen?
  • Are paragraph breaks in the article kept?
  • Should the functions print the lines or return them, and do trailing spaces matter?

Part 1 — A fixed number of lines

Given the text and a number of lines k, lay the text out in k lines and print them.

Clarifying Questions for this Part Guidance

  • Must the layout use exactly k lines, or at most k ? What if the text has fewer than k words?
  • Should the lines be as even as possible in length, or in number of words?

What This Part Should Cover Guidance

  • A precise objective for the layout, agreed before coding
  • An algorithm that meets it, with its complexity
  • Justifying the resulting lines, and handling k larger than the number of words

Part 2 — A fixed width

Given a width W, break the text into lines of at most W characters, justify them, and print them.

What This Part Should Cover Guidance

  • Filling lines with a length check that counts the separating spaces correctly
  • Distributing spaces within a line, single-word lines, and the last line
  • Printing every line exactly once, including the final one

Part 3 — Multiple columns

Lay the article out in several columns side by side, as on a newspaper page, and print the page row by row.

Clarifying Questions for this Part Guidance

  • Is the number of columns given, or the column height? What are the column width and the gap between columns?
  • Do lines fill the first column from top to bottom before moving to the next?
  • How are rows printed when the last column is shorter than the others?

What This Part Should Cover Guidance

  • Reusing the fixed-width line breaking for each column's width
  • Mapping a line's index to a column and a row, and padding short columns
  • Printing row by row with the columns aligned

What a Strong Answer Covers Guidance

  • Precise layout rules and a worked example agreed before coding each part
  • One line-breaking routine shared by all three parts
  • Exact widths: no off-by-one errors in counting spaces, and correct padding
  • Tests for edge cases: an empty text, a single word, a word exactly as long as the width
  • Debugging by tracing a small example, and a clear complexity analysis

Follow-up Questions Guidance

  • How would you choose line breaks that minimize total raggedness (for example the sum of squared trailing spaces) instead of filling each line greedily?
  • How would you handle a word longer than the width?
  • If the page has a fixed height, how would Part 3 decide the number of columns and continue onto the next page?
  • How would you lay out a very long article without holding every line in memory?