← Google Interview Insights

Google·Software Engineer·Onsite - Coding / Algorithms·Junior

JuniorPrefer not to say
May 2026

Summary

Google onsite coding round for a new grad SWE position. One problem the whole session, but it had enough edge cases to keep things interesting for longer than I expected.

Questions Asked (1)

Q1

Given a string and a max line width, implement a word-wrap function that returns the number of lines needed to render the text, correctly handling explicit newline characters and consecutive whitespace.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

Framed as 'imagine a Google Doc page with a fixed character width.' Sounds straightforward until you realize how many edge cases pile up: what if there's a newline right after another newline (empty line still counts), what if the input starts with whitespace, what if a word lands exactly at the width limit.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the exact semantics of word wrapping, especially how to handle explicit newlines and consecutive whitespace, then design a linear-time algorithm that processes the string character by character while tracking the current line length. Discuss edge cases and trade-offs, and finally implement and test the solution.

Pro tip: Explicitly state your assumptions about whitespace handling (e.g., whether consecutive spaces collapse) and newline behavior (e.g., whether they force a line break) before coding, as interviewers often evaluate how you handle ambiguity.

1. Clarify requirements and edge cases

Ask questions to pin down how newlines and consecutive whitespace should affect line breaks and word separation. Confirm whether words can be split and how trailing spaces are treated.

2. Design the algorithm

Choose a linear scan approach that tracks the current line length and word boundaries. Decide how to handle explicit newlines (force line break) and consecutive whitespace (collapse or preserve).

3. Implement the solution

Write clean code that iterates through the string, updating line count when the width is exceeded or a newline is encountered. Handle whitespace according to the clarified rules.

4. Test with examples and edge cases

Run through provided examples and additional edge cases (empty string, single long word, multiple newlines, leading/trailing spaces) to verify correctness.

5. Discuss trade-offs and optimizations

Mention time/space complexity (O(n) time, O(1) space) and any alternative approaches (e.g., splitting into words first) with their pros and cons.

Key Points to Mention

  • Handling explicit newline characters as forced line breaks
  • Treatment of consecutive whitespace (collapse vs. preserve)
  • Time and space complexity of the solution (O(n) time, O(1) space)
  • Edge cases: empty string, single word longer than max width, multiple newlines
  • Whether words can be split across lines (usually not, but clarify)
  • Trade-offs between character-by-character scanning and splitting into words first

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