← Uber Interview Insights

Uber·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

Uber coding screen for a software engineer role, one algorithmic problem, felt like a bit of a brain teaser dressed up as a bit manipulation question.

Questions Asked (1)

Q1

Given a positive integer n, you can add or subtract any power of 2 in a single operation. What is the minimum number of operations needed to reduce n to zero?

Algorithms & Data Structures
Author's notes

Took me a minute to see this wasn't just a greedy bit-count problem.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Recognize that this is a greedy problem where at each step you choose to add or subtract the largest power of 2 that minimizes the absolute difference to the nearest multiple of that power. Alternatively, use dynamic programming with states based on the binary representation. The optimal strategy is to always round n to the nearest multiple of the current highest power of 2, which leads to a logarithmic number of operations.

Pro tip: Start by explaining the greedy choice: for the highest power of 2 less than or equal to n, decide whether adding or subtracting it gets you closer to a multiple of twice that power. This shows you understand the trade-off and can optimize without brute force.

1. Understand the problem

Clarify that you can add or subtract any power of 2 (1, 2, 4, 8, ...) in one operation, and you want to reach zero with the fewest operations. Note that powers can be used multiple times.

2. Identify the greedy strategy

Observe that at each step, you should use the largest power of 2 that is at most n, and decide whether to add or subtract it based on which brings you closer to a multiple of the next higher power of 2. This minimizes the remaining distance.

3. Derive the recurrence

Define f(n) as the minimum operations for n. If n is a power of 2, f(n)=1. Otherwise, let p be the largest power of 2 ≤ n. Then f(n) = 1 + min(f(n-p), f(p*2 - n)). This captures the choice of subtracting p or adding (p*2 - n) to reach a multiple of 2p.

4. Analyze complexity and optimize

Show that the recurrence reduces n by at least half each time, leading to O(log n) time and O(log n) space if implemented recursively. Alternatively, use bit manipulation to compute the answer in O(log n) by counting the number of 1s in the binary representation with adjustments for carries.

5. Test with examples

Walk through small examples like n=3 (2 ops: 3-2=1, 1-1=0), n=7 (3 ops: 7+1=8, 8-8=0? Actually 7+1=8, then 8-8=0 is 2 ops? Wait, 7+1=8 (1 op), 8-8=0 (1 op) total 2 ops? But 7-4=3, 3-2=1, 1-1=0 is 3 ops. So 2 ops is better. Check n=15: 15+1=16, 16-16=0 (2 ops). So pattern: for numbers just below a power of 2, adding to reach the next power is efficient.)

Key Points to Mention

  • Greedy choice: always round to the nearest multiple of the current highest power of 2.
  • Dynamic programming recurrence: f(n) = 1 + min(f(n-p), f(2p - n)) where p is the largest power of 2 ≤ n.
  • Time complexity: O(log n) because n reduces by at least half each step.
  • Connection to binary representation: the answer relates to the number of 1s and carries when adding 1.
  • Edge cases: n=0 (0 ops), n=1 (1 op), n=2^k (1 op).
  • Proof of optimality: exchange argument showing that any optimal solution can be transformed to use the greedy choice.

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