โ† PayPal questions
CodingEasy

Word count of the longest line in a file

Filed under PayPal

Free read 2 of 3

Open the file and read the first three lines using a buffer or iterator.

The question

Read a file, extract the first three lines, determine which of these lines is the longest, and return the word count of that line.

Constraints

  • Only consider the first three lines of the file.
  • Word count is defined by whitespace-separated tokens.
  • Handle files with fewer than three lines.

Examples

Input
Line 1: 'Hello world' Line 2: 'A' Line 3: 'Test line here'
Output
3
Why
Line 3 has 3 words, which is the longest.

Approach

  1. Open the file and read the first three lines using a buffer or iterator.

Tips

  • What they are testing: Tests file I/O handling and string manipulation.
  • Common mistake: Reading the entire file into memory instead of processing line by line.

Related PayPal questions

Collected from public candidate posts and coding forums. Updated Nov 2026.