← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Google coding interview for a software engineer role, centered on a text wrapping problem that looked straightforward but had a bunch of edge cases hiding in it. The kind of question where you think you're done and then they ask about one more thing.

Questions Asked (1)

Q1

Given a string of text and a maximum line width (in characters), calculate how many lines are needed to display the text. You must wrap only at whitespace boundaries, respect explicit newline characters, and handle leading, trailing, and extra internal whitespace. Also discuss what happens when a single word is longer than the max width.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

I started okay, got the basic word-wrapping loop down pretty fast.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the requirements and edge cases, then propose a greedy line-by-line simulation that tokenizes the text while preserving explicit newlines and handling whitespace normalization. Discuss the special case of a word longer than the max width, and analyze time/space complexity.

Pro tip: Explicitly state your assumptions about whitespace normalization (e.g., collapsing multiple spaces) and confirm them with the interviewer before coding. This shows attention to detail and avoids misalignment on ambiguous requirements.

1. Clarify Requirements and Edge Cases

Ask about whitespace handling (collapse multiple spaces? trim leading/trailing?), newline behavior (reset line count? preserve empty lines?), and the definition of a 'word' (split on spaces only or all whitespace?). Confirm how to handle words longer than max width.

2. Outline the Algorithm

Describe a greedy approach: iterate through the text, building lines by adding words until the next word would exceed the max width, then start a new line. Handle explicit newlines by resetting the current line and incrementing the line count.

3. Handle Whitespace and Newlines

Explain how you normalize whitespace: split on whitespace, ignore empty tokens, and treat newlines as forced line breaks. Discuss whether to trim leading/trailing spaces and how to handle multiple consecutive newlines.

4. Address Long Words

If a word exceeds max width, decide whether to break it (e.g., split into chunks) or place it on its own line. State the chosen behavior and its implications (e.g., breaking words may require hyphenation or not).

5. Analyze Complexity and Test

State time complexity O(n) where n is the number of characters, and space O(1) if processing in a streaming fashion. Walk through test cases: empty string, single long word, multiple newlines, and mixed whitespace.

Key Points to Mention

  • Whitespace normalization: collapsing multiple spaces and trimming leading/trailing whitespace.
  • Explicit newline handling: newlines force a line break and reset the current line length.
  • Greedy line packing: add words until the next word would exceed max width.
  • Long word handling: either break the word or place it on its own line, and discuss trade-offs.
  • Edge cases: empty string, only whitespace, multiple consecutive newlines, and words exactly at max width.
  • Time and space complexity: O(n) time, O(1) extra space if processing in a streaming manner.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.