FastPrepMinimum Operations To Reduce To Zero
Problems/Minimum Operations To Reduce To Zero

Minimum Operations To Reduce To Zero

Uber logoUber● MediumOA

Updated Sep 2026

QuestionFunctionExamples

Problem statement

Given a positive integer n, you can either add or subtract 2^i in a single operation where i >= 0. Determine the minimum number of operations required to reduce n to 0.

Find the minimum number of operations required to convert n to 0.

  • Constraints 1 <= n < 2^40

Function

getMinOperations(n: long) → int

Examples

Example 1

n = 6return = 2

n can be reduced to 0 using two operations:

  • Choose i = 1 and subtract 2^1 from 6, so 6 - 2 = 4.
  • Choose i = 2 and subtract 2^2 from 4, so 4 - 4 = 0.

The answer is 2.

Example 2

n = 21return = 3

One sequence of operations is:

  • Choose i = 0, subtract 2^0 = 1 from 21, converting it to 20.
  • Choose i = 2, subtract 2^2 = 4 from 20, converting it to 16.
  • Choose i = 4, subtract 2^4 = 16 from 16, converting it to 0.

More Uber Online Assessment questions

  • Count Palindromic Ancestor PathsOA · Seen Aug 2026
  • Jump Game with Prime-3 StepsOA · Seen Aug 2026
  • Last Truck to Leave the LaneOA · Seen Jul 2026
  • Chain of CommandOA · Seen Jul 2026
  • Total Palindrome Substring CostOA · Seen Jun 2026
  • Jump Game with Prime-Step RuleOA · Seen May 2026
  • Pipeline Throughput Maximization Under BudgetOA · Seen May 2026
  • Balanced Prefix Sets in a PermutationOA · Seen Apr 2026
  • Minimum Edge Reversals to Root a TreeOA · Seen Apr 2026
  • Maximum Riders in CarOA · Seen Apr 2026

Similar questions at other companies

  • Minimum Halvings for Alternating ParityIBM · OAIBM interview questions
  • XOR MultiplicationMicrosoft · OAMicrosoft interview questions
  • Get Minimum OperationsJP Morgan Chase · OAJP Morgan Chase interview questions
  • Minimum Power-of-Two AdjustmentsNVIDIA · OANVIDIA interview questions
  • Match an XOR-Adjusted SumSquarepoint Capital · OASquarepoint Capital interview questions
  • Integer BreakAmazon · ONSITE INTERVIEW
  • Minimum Cost for a Target WeightPace Stock Broking Services · PHONE SCREEN
  • IP Address to CIDR BlocksOpenAI · PHONE SCREENOpenAI interview questions
  • Minimum Rounds to Complete All TasksInfosys · OAInfosys interview questions
  • Server Upgrade PlanningWells Fargo · OAWells Fargo interview questions
All Uber interview questionsSee Uber hiring insights
Saved
class Solution {
    public int getMinOperations(long n) {
      // write your code here
    }

}
Checking account…