Word Wrap Line Count with Whitespace and Newlines

Asked at Google · Coding Questions · Difficulty: Easy · Last asked Feb 1, 2026

A page has a fixed width measured in characters. Given a block of text, lay it out line by line and return how many lines the page needs. Words are packed greedily, explicit newlines always start a new line, and runs of other whitespace count as one gap.

Loading the problem…