← Google Interview Insights

Google·Software Engineer·Online Assessment (OA)·Intermediate

Intermediate
May 2026

Summary

Google SWE online assessment with a bit math/greedy problem. Nothing too wild but the constraint range (up to 10^18) means you can't brute force it, which is where most people probably trip up.

Questions Asked (1)

Q1

Given a positive integer n (up to 10^18), find the minimum number of operations to reduce it to 0. If n is even you divide by 2; if n is odd you can either add 1 or subtract 1.

Algorithms & Data Structures
Author's notes

The small examples make it feel easy and then you see the constraint is 10^18 and suddenly brute force or recursion with memoization starts looking sketchy.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Recognize this as a greedy problem where the optimal choice for odd n depends on n mod 4: subtract 1 if n ≡ 1 (mod 4), add 1 if n ≡ 3 (mod 4), with special handling for n=3. Then simulate the process efficiently using bitwise operations or a loop that runs in O(log n) time, which is feasible for n up to 10^18.

Pro tip: Mention the edge cases explicitly (n=1, n=2, n=3) and explain why the greedy choice works by analyzing the binary representation—this shows you understand the underlying pattern, not just memorized rules.

1. Clarify the problem and constraints

Restate the problem to ensure understanding: for even n, divide by 2; for odd n, choose +1 or -1 to minimize total operations to reach 0. Note that n can be up to 10^18, so an O(n) simulation is impossible; we need an O(log n) approach.

2. Identify the greedy strategy

For odd n, the optimal move depends on n mod 4: if n ≡ 1 (mod 4), subtract 1; if n ≡ 3 (mod 4), add 1 (except when n=3, where subtract 1 is better). This ensures that after the operation, the number is divisible by 4, leading to more divisions by 2.

3. Handle base cases and edge cases

Explicitly handle small values: n=0 (0 ops), n=1 (1 op: subtract 1), n=2 (2 ops: divide to 1, subtract to 0), n=3 (2 ops: subtract to 2, divide to 1, subtract to 0—or add to 4, divide to 2, divide to 1, subtract to 0 gives 4 ops, so subtract is better).

4. Simulate efficiently

Use a while loop that repeatedly applies the rule: if n is even, n /= 2; if odd, apply the mod 4 rule (with n=3 as exception). Count operations. Since each step reduces n by at least half, the loop runs in O(log n) time.

5. Analyze complexity and prove correctness

Explain that the algorithm runs in O(log n) time and O(1) space. Justify the greedy choice by showing that choosing the move that makes n divisible by 4 maximizes the number of subsequent divisions by 2, which minimizes total operations.

Key Points to Mention

  • Greedy choice based on n mod 4: subtract 1 when n ≡ 1 (mod 4), add 1 when n ≡ 3 (mod 4).
  • Special case n=3: subtract 1 is optimal (2 operations) rather than add 1 (4 operations).
  • Time complexity O(log n) due to halving, suitable for n up to 10^18.
  • Space complexity O(1) with iterative simulation.
  • Proof of correctness: making n divisible by 4 allows two consecutive divisions by 2, reducing operations.
  • Edge cases: n=0, n=1, n=2, n=3.

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