FastPrepMinimum Operations to Reduce an Integer to Zero
Problems/Minimum Operations to Reduce an Integer to Zero

Minimum Operations to Reduce an Integer to Zero

Microsoft logoMicrosoft● MediumFULLTIMEOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Microsoft online assessment. The report names the task without its full statement or limits. Our version is about 90% the same as the reported question.

Given a positive integer n, return the minimum number of operations needed to reduce it to zero.

In one operation, you may add or subtract 2^b for any nonnegative integer b. Intermediate values may be larger than the starting value. Count only these additions and subtractions; rewriting a number in binary is not an operation.

Function

minOperations(n: int) → int

Examples

Example 1

n = 39return = 3

One optimal sequence is 39 - 32 = 7, 7 + 1 = 8, then 8 - 8 = 0, using 3 operations.

Example 2

n = 54return = 3

Use 54 + 2 = 56, 56 + 8 = 64, then 64 - 64 = 0.

Example 3

n = 8return = 1

Subtract 2^3 = 8 once.

Constraints

  • For this exercise, assume 1 <= n <= 10^5.
  • Each operation adds or subtracts one power of two.

More Microsoft Online Assessment questions

  • Minimum Cost to Process Priority TasksOA · Seen Nov 2026
  • Most Visited Sprint MarkerOA · Seen Nov 2026
  • Maximum Escape Game ScoreOA · Seen Nov 2026
  • Count Subarrays with a Perfect-Square ProductOA · Seen Nov 2026
  • Maximum Interactive Team SizeOA · Seen Nov 2026
  • Maximize Pipeline Throughput Under a Scaling BudgetOA · Seen Nov 2026
  • Minimum Cost K-Capable ModelsOA · Seen Nov 2026
  • Find Available Meeting RoomsOA · Seen Oct 2026
  • Maximum Sum of a K-StarOA · Seen Oct 2026
  • Capacity to Ship Packages Within DaysOA · ONSITE INTERVIEW · Seen Oct 2026

Similar questions at other companies

  • Minimum No-Repeat Segments After One Character RemovalSalesforce · OASalesforce interview questions
  • Minimize the Maximum Array ElementChubb · OAChubb interview questions
  • Minimum Halvings for Alternating ParityIBM · OAIBM interview questions
  • Server SelectionPoint72 · OAPoint72 interview questions
  • Minimum Servers for an Exact LoadJP Morgan Chase · OAJP Morgan Chase interview questions
  • Minimum Operations To Reduce To ZeroUber · OAUber interview questions
  • Maximize PartitionsAmazon · OA
  • Maximum XOR SuffixTikTok · OATikTok interview questions
  • Minimum Power-of-Two AdjustmentsNVIDIA · OANVIDIA interview questions
  • Fewest Steps to Build an Array from ZerosExpedia · OAExpedia interview questions

Interview reports that mention this question

  • Microsoft OA Questions, October 2026: 4 Experiences

Microsoft interview experiences

  • Microsoft Experienced SWE Phone Screen: 1 Experience
  • Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
  • Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
  • Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
  • Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)
All Microsoft interview questionsSee Microsoft hiring insights
Saved
class Solution {
    public int minOperations(int n) {
        // write your code here
    }
}
Checking account…