Count the lines a terminal needs to print a paragraph at two words per line

Quick Overview

Count how many lines a terminal needs to print a paragraph when each line holds at most two words. This warm-up tests splitting a string into words and rounding a division up correctly when the word count is odd.

Count the lines a terminal needs to print a paragraph at two words per line

Company: Intel

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a paragraph represented as a string of words, a terminal can only write two words per line. Calculate the total number of lines needed to print the entire paragraph. Words in the paragraph are separated by single spaces. If the total number of words is odd, the last line will contain only one word. ### Function Signature ```python def count_lines(paragraph: str) -> int: ``` ### Rules - A word is a maximal run of non-space characters. - Words are printed in their original order, two per line, so every line except possibly the last holds exactly two words. ### Constraints - `1 <= len(paragraph) <= 10^4` - The paragraph contains only lowercase English letters and spaces. - Words are separated by single spaces. - There are no leading or trailing spaces. ### Examples ```text Input: paragraph = "hello world foo bar baz" Output: 3 ``` There are 5 words. Line 1 is "hello world", line 2 is "foo bar" and line 3 is "baz", so 3 lines are needed. ```text Input: paragraph = "the quick brown fox" Output: 2 ``` There are 4 words. Line 1 is "the quick" and line 2 is "brown fox". ```text Input: paragraph = "a" Output: 1 ``` A single word still needs one line of its own.

Overview: Count how many lines a terminal needs to print a paragraph when each line holds at most two words. This warm-up tests splitting a string into words and rounding a division up correctly when the word count is odd.

Intel logo
Intel
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given a paragraph represented as a string of words, a terminal can only write two words per line. Calculate the total number of lines needed to print the entire paragraph.

Words in the paragraph are separated by single spaces. If the total number of words is odd, the last line will contain only one word.

Function Signature

def count_lines(paragraph: str) -> int:

Rules

  • A word is a maximal run of non-space characters.
  • Words are printed in their original order, two per line, so every line except possibly the last holds exactly two words.

Constraints

  • 1 <= len(paragraph) <= 10^4
  • The paragraph contains only lowercase English letters and spaces.
  • Words are separated by single spaces.
  • There are no leading or trailing spaces.

Examples

Input: paragraph = "hello world foo bar baz"
Output: 3

There are 5 words. Line 1 is "hello world", line 2 is "foo bar" and line 3 is "baz", so 3 lines are needed.

Input: paragraph = "the quick brown fox"
Output: 2

There are 4 words. Line 1 is "the quick" and line 2 is "brown fox".

Input: paragraph = "a"
Output: 1

A single word still needs one line of its own.