← Microsoft Interview Insights

Microsoft·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jun 2026

Summary

Microsoft SWE coding round, one problem the whole time. Pretty focused session, just bit manipulation and greedy thinking under pressure.

Questions Asked (1)

Q1

Given a non-negative integer target, you start at 0 and can add or subtract any power of two in each operation. What is the minimum number of operations to reach the target?

Algorithms & Data Structures
Author's notes

My first instinct was just greedy from the top, grab the largest power of two that fits and subtract.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Recognize that this is a shortest path problem on an infinite graph where each move adds or subtracts a power of two. Use BFS from 0 to target, but optimize by considering only powers of two up to the next power above target. Alternatively, derive a greedy or DP solution based on binary representation.

Pro tip: Mention that the problem can be solved in O(log target) time using a greedy approach that examines the binary representation and handles carries, similar to counting set bits but with the option to subtract. This shows you can optimize beyond brute force.

1. Clarify the problem

Confirm that operations are adding or subtracting any power of two (including 2^0=1) and that target is non-negative. Ask if there are constraints on target size.

2. Model as a graph

View each integer as a node, with edges to n ± 2^k for all k ≥ 0. The problem reduces to finding the shortest path from 0 to target.

3. Consider BFS with bounds

Since powers of two grow exponentially, only powers up to the smallest power of two greater than target are needed. BFS from 0 will find the minimum operations, but may be inefficient for large targets.

4. Optimize with binary representation

Observe that the optimal sequence corresponds to representing target in binary with digits -1, 0, 1 (signed binary). Use a greedy algorithm from least significant bit to most, deciding whether to add or subtract based on the bit and carry.

5. Analyze complexity and edge cases

The greedy approach runs in O(log target) time and O(1) space. Handle target=0 (0 operations) and ensure the algorithm works for all non-negative integers.

Key Points to Mention

  • The problem is equivalent to finding the minimum number of signed powers of two that sum to target.
  • BFS is a straightforward solution but can be optimized using the binary representation.
  • Greedy algorithm: process bits from least significant to most, maintaining a carry; if bit is 1, add 1 operation and decide to carry based on next bit.
  • The answer is related to the number of non-zero digits in the non-adjacent form (NAF) of the target.
  • Time complexity can be O(log target) with the optimized approach.
  • Edge case: target=0 requires 0 operations.

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