← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

Google SWE interview with a text-wrapping problem that starts simple and then gets a follow-up that actually requires some real thinking. Nothing behavioral, just straight algorithms the whole time.

Questions Asked (2)

Q1

Given a fixed page width W (in characters) and a string of space-separated words, write a function that counts how many lines are needed to render the text using greedy word wrapping (no breaking words across lines).

Algorithms & Data Structures
Author's notes

Seemed straightforward and I got through the greedy logic fine.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying edge cases like empty string, single word longer than W, and multiple spaces. Then describe a greedy algorithm that iterates through words, maintaining the current line length and incrementing the line count when adding the next word would exceed W. Finally, discuss time and space complexity and potential optimizations.

Pro tip: Mention that you would handle the case where a single word exceeds W by either truncating or allowing it to overflow, and ask the interviewer which behavior they prefer. This shows attention to detail and real-world constraints.

1. Clarify requirements and edge cases

Ask about empty input, words longer than W, multiple spaces, and whether trailing spaces count. Confirm that words are separated by single spaces.

2. Outline the greedy algorithm

Explain that you'll iterate through words, keeping track of current line length. If adding a word (plus a space if not first) exceeds W, start a new line and increment line count.

3. Walk through an example

Use a small example like W=10 and 'hello world foo bar' to demonstrate how the algorithm works step by step, showing line breaks and final count.

4. Analyze complexity and discuss optimizations

State that time complexity is O(n) where n is number of words, and space is O(1) extra. Mention that you could avoid splitting the string by scanning characters, but splitting is simpler.

5. Write clean code and test

Implement the function with clear variable names, handle edge cases, and mentally test with examples including empty string and single long word.

Key Points to Mention

  • Greedy approach: always fit as many words as possible on the current line before wrapping.
  • Edge cases: empty string, single word longer than W, multiple consecutive spaces.
  • Time complexity O(n) and space complexity O(1) beyond input storage.
  • Handling of spaces: only add a space between words when the line is not empty.
  • Potential ambiguity: what to do if a word exceeds W? Ask interviewer.
  • Alternative: process character by character without splitting, but splitting is more readable.

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

Q2

Follow-up: if you have two paragraphs displayed side by side sharing a total width W, how do you find the column split (w1 and w2, where w1 + w2 = W) that minimizes the maximum number of lines across both columns?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This is where I started sweating.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the problem as minimizing the maximum of two functions f(w1) and g(W-w1), where f and g represent the number of lines each paragraph takes given a width. Use binary search on the answer (the maximum number of lines) or ternary search on w1 to find the optimal split, leveraging the monotonicity of line counts with respect to width.

Pro tip: Mention that the line count function is a step function (non-increasing as width increases), so the maximum of the two is unimodal or can be handled with binary search on the answer, and discuss edge cases like very narrow or very wide columns.

1. Define the objective function

Let L1(w) be the number of lines paragraph 1 takes at width w, and L2(w) similarly. We want to minimize max(L1(w1), L2(W-w1)) over w1 in [0, W].

2. Analyze monotonicity

L1(w) is non-increasing as w increases (wider column → fewer lines), and L2(W-w1) is non-decreasing as w1 increases. Thus the maximum of the two is minimized where they cross or at the boundary.

3. Choose an algorithm

Use binary search on the answer: for a candidate max lines M, check if there exists w1 such that L1(w1) ≤ M and L2(W-w1) ≤ M. Alternatively, ternary search on w1 if the function is unimodal.

4. Implement the check

For a given M, find the range of widths where L1(w) ≤ M (i.e., w ≥ w1_min) and where L2(W-w) ≤ M (i.e., w ≤ w1_max). If w1_min ≤ w1_max, then M is feasible.

5. Handle discrete widths and efficiency

Since widths are likely integers (pixels or characters), binary search over integer w1 or over M. Precompute line counts for all possible widths to make checks O(1).

Key Points to Mention

  • Monotonicity of line count with respect to column width
  • Binary search on the answer (minimax) or ternary search on the split point
  • Feasibility check: existence of w1 satisfying both line constraints
  • Time complexity: O(W log W) or O(W log maxLines) with precomputation
  • Edge cases: extremely narrow columns, one column empty, non-integer widths
  • Trade-offs: exact vs approximate solutions, precomputation vs on-the-fly

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