FastPrepCount Lines in a Width-Limited Document
Problems/Count Lines in a Width-Limited Document

Count Lines in a Width-Limited Document

Google logoGoogle● MediumNEW GRADPHONE SCREENONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Google interview reports ask for the number of lines when text is wrapped greedily. They cover explicit newlines, and a follow-up where a word is wider than the page and must be split. We defined monospace width, how spaces are cleaned up, empty paragraphs, and how the last piece is shared. Our version is about 92% the same as the combined reported questions.

Given document text text and a maximum line width maxWidth, return the number of display lines produced by greedy word wrapping.

  • Every character has width one.
  • Spaces and tabs separate words. Consecutive horizontal whitespace is displayed as one space, and leading or trailing horizontal whitespace is ignored.
  • A newline starts a new paragraph. Every empty paragraph contributes one blank display line.
  • Within each non-empty paragraph, place as many words as possible on each line with one display space between adjacent words.
  • If a word is longer than maxWidth, first move it to a new line when the current line is non-empty, then split it into consecutive width-maxWidth chunks. Its final shorter chunk may share a line with the next word under the ordinary one-space rule.

Empty text contains zero display lines.

Function

countWrappedLines(text: String, maxWidth: int) → int

Examples

Example 1

text = "Google   builds\nfast tools"maxWidth = 10return = 3

The first paragraph wraps into Google and builds. The second paragraph fits as fast tools.

Example 2

text = "abcdef x"maxWidth = 3return = 3

The oversized word becomes abc and def; x needs a third line.

Example 3

text = "\n"maxWidth = 5return = 2

One newline separates two empty paragraphs, and each contributes one blank line.

Constraints

  • 0 <= text.length <= 200000.
  • 1 <= maxWidth <= 100000.
  • text contains printable characters, spaces, tabs, and newline characters.

More Google Onsite Interview questions

  • Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
  • Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
  • Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
  • Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
  • Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Choose the Next Hidden-Word GuessFlexport · ONSITE INTERVIEW
  • Choose the Next Hidden-Word GuessCharacter.AI · PHONE SCREEN
  • URL Segment Compression Part 3 — Global Token BudgetStripe · ONSITE INTERVIEW
  • Greedy and Beam Sentence DecodingMicrosoft · PHONE SCREEN · ONSITE INTERVIEW
  • Text JustificationUber · PHONE SCREEN
  • Word Wrap with Even JustificationSentry · OA · PHONE SCREEN
  • Format a Centered Newspaper PageVisa · OA
  • Minimum Steps on a Circular Letter DialIBM · OA
  • Max Number of OperationsSalesforce · OA
  • Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEW
See Google hiring insights
Saved
class Solution {
    public int countWrappedLines(String text, int maxWidth) {
        // Write your code here.
    }
}
Checking account…