← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Senior

Senior
May 2026

Summary

Google SWE interview with a pretty gnarly computational geometry question. One round, one problem, lots of follow-ups that kept branching in directions I didn't fully anticipate.

Questions Asked (1)

Q1

You have a 2D grid with n non-overlapping axis-aligned square cakes, each with a real-valued top-left corner and an integer side length. Design an algorithm to find a horizontal line y* such that the total cake area strictly above the line equals the total area strictly below it. Walk through the search interval, prove a solution exists, prove monotonicity, give time and space complexity in terms of n and precision epsilon, and discuss numerical stability and stopping criteria for floating-point coordinates. If multiple solutions exist, explain how you'd return one. Then extend your approach to handle (a) overlapping squares and (b) axis-aligned rectangles instead of squares.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This one took me a minute to even parse.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Define a function f(y) = area above y - area below y, which is continuous and monotonically decreasing from total area to -total area, guaranteeing a root. Use binary search on y within the grid's vertical bounds, iterating until the interval width is less than epsilon, and compute f(y) efficiently by summing contributions from each square. Then extend to overlapping squares by handling intersections and to rectangles by adjusting area formulas.

Pro tip: Emphasize that the problem reduces to finding a root of a monotonic function, and that binary search is optimal because f(y) can be evaluated in O(n) time. Mention that for overlapping squares, you can still compute f(y) in O(n) by summing individual contributions, but for rectangles, the same holds; however, if you need to handle arbitrary overlaps, you might need a sweep line or inclusion-exclusion, but since f(y) is linear in y, the sum of individual contributions works.

1. Define the function and prove monotonicity

Define f(y) = total area above y - total area below y. Show that f is continuous and strictly decreasing (or non-increasing) because as y increases, area above decreases and area below increases. Thus, f(y) goes from total area (at y below all cakes) to -total area (at y above all cakes), so by IVT a root exists.

2. Design binary search algorithm

Set initial interval [y_min, y_max] covering all cakes. While (y_max - y_min) > epsilon, compute mid = (y_min + y_max)/2, evaluate f(mid) in O(n) by summing each cake's area above and below mid. If f(mid) > 0, set y_min = mid; else set y_max = mid. Return y* = (y_min + y_max)/2.

3. Analyze complexity and numerical stability

Time complexity: O(n log((y_max - y_min)/epsilon)). Space: O(1) extra. Discuss floating-point precision: use epsilon as tolerance, avoid exact equality, and consider using relative error. Stopping criteria: when interval width < epsilon or when |f(mid)| < delta.

4. Handle multiple solutions and extensions

If multiple solutions exist (e.g., f(y)=0 over an interval), binary search will find one; return any. For overlapping squares, f(y) is still monotonic and can be computed by summing individual contributions (since area above is additive even with overlaps). For rectangles, same approach works; just adjust area formulas for width and height.

5. Discuss edge cases and optimizations

Mention edge cases: cakes entirely above/below line, zero-area cakes. For large n, consider sorting events or using a sweep line to evaluate f(y) faster if needed, but O(n) per evaluation is acceptable. For rectangles, if they overlap, the sum of individual areas above still gives correct total area above because area is additive.

Key Points to Mention

  • Monotonicity of f(y) and existence of solution via Intermediate Value Theorem
  • Binary search on continuous domain with epsilon precision
  • O(n) evaluation of f(y) per iteration by summing per-cake contributions
  • Time complexity O(n log(1/epsilon)) and space O(1)
  • Numerical stability: use epsilon tolerance, avoid exact comparisons, consider relative error
  • Extensions: overlapping squares and rectangles still work because area is additive; adjust area formulas accordingly

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