← Uber Interview Insights

Uber·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Apr 2026

Summary

Uber SWE coding round with a bit representation problem that looks deceptively clean until you realize the optimal path isn't just counting set bits.

Questions Asked (1)

Q1

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

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

My first instinct was just count the 1-bits in the binary representation and call it a day.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Recognize that the problem is equivalent to finding the minimum number of signed powers of two that sum to n, which can be solved using a greedy strategy from the least significant bit: if the current bit is 1, add 1 operation and move to the next bit; if the current bit is 0, do nothing. Alternatively, use dynamic programming on the binary representation, considering carries, to achieve O(log n) time.

Pro tip: After presenting the greedy solution, mention that it can be optimized to O(log n) by processing bits and handling carries, and that the problem is essentially finding the minimal Hamming weight of n in signed binary representation (non-adjacent form).

1. Understand the problem

Clarify that each operation adds or subtracts a power of two, and we want to reduce n to zero with minimum operations. This is equivalent to representing n as a sum of signed powers of two with minimal terms.

2. Explore small examples

Try n=1 (1 op), n=2 (1 op), n=3 (2 ops: 4-1), n=7 (2 ops: 8-1), n=15 (2 ops: 16-1). Notice that numbers of the form 2^k - 1 take 2 operations, while others may take more.

3. Derive a greedy strategy

Process bits from least significant to most significant. If the current bit is 1, we can either subtract 2^i (cost 1) and move on, or add 2^i to create a carry. Greedy choice: if the next bit is also 1, adding 2^i and carrying is better; otherwise, subtracting is better.

4. Implement and analyze complexity

Write a function that iterates through bits, maintaining a carry, and counts operations. Time complexity O(log n), space O(1).

5. Discuss trade-offs and edge cases

Compare with dynamic programming (O(log n) states) and mention that the greedy approach is optimal. Handle n=0 (0 ops) and large n efficiently.

Key Points to Mention

  • The problem is equivalent to finding the minimum number of signed powers of two summing to n.
  • Greedy approach: process bits from LSB to MSB, using carry to minimize operations.
  • Optimal strategy: when consecutive 1s are encountered, adding a power of two and carrying reduces operations.
  • Time complexity O(log n) and space O(1) for the greedy solution.
  • Connection to non-adjacent form (NAF) and minimal Hamming weight in signed binary representation.
  • Edge cases: n=0, n=1, n=2^k, n=2^k - 1.

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