FastPrepBroken Calculator
Problems/Broken Calculator

Broken Calculator

Nutanix logoNutanix● MediumINTERNOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Two reports from early-2020 Nutanix intern online assessments include this question. One spells it out. A calculator has only a minus-one key and a doubling key that work. You get a start number and a target, and you need the fewest presses to get from one to the other. Both numbers are from 1 to 10^4. That report also sketches a table-based answer. The other report only names the question.

Neither report gives examples or a function signature. Here you return the fewest presses as an integer, the screen can hold any whole number, and the examples are our own.

Our version is about 95% the same as the reported questions.

A calculator shows the number n. Only two of its keys still work:

  • Minus one subtracts 1 from the number on the screen.
  • Double multiplies the number on the screen by 2.

Return the fewest key presses that make the screen show m. If n already equals m, the answer is 0.

The screen can show any whole number, so a number larger than m on the way is fine.

Function

minKeyPresses(n: int, m: int) → int

Examples

Example 1

n = 3m = 10return = 3

Press double, minus one, double: 3 → 6 → 5 → 10. That is 3 presses. Doubling twice first (3 → 6 → 12) and then pressing minus one twice also reaches 10, but uses 4 presses. No sequence of 2 presses works, so the answer is 3.

Example 2

n = 7m = 4return = 3

The start is already larger than the target. Doubling only makes the number bigger, so press minus one 3 times: 7 → 6 → 5 → 4. The answer is 3.

Example 3

n = 5m = 17return = 4

One best sequence is 5 → 10 → 9 → 18 → 17 (double, minus one, double, minus one). It goes above 17 on the way. The answer is 4.

Constraints

  • 1 <= n, m <= 10^4

More Nutanix Online Assessment questions

  • Good SubsequencesOA · Seen Jul 2026
  • Min Cost to Connect All PointsOA · Seen Jun 2026
  • Digit Changes for a Multiple of 9OA · Seen Aug 2025
  • Gas Station Loop StartOA · Seen Aug 2025
  • Number of Players (for MTS2)OA · Seen Oct 2024
  • Work Schedules (for MTS2)OA · Seen Oct 2024
  • Minimum Operations to Convert ArrayOA · Seen Apr 2024
  • No Adjacent CharactersOA · Seen Apr 2024
  • Build a BST From a ListOA · Seen Nov 2022
  • Delete Even-Valued Nodes From a Linked ListOA · Seen Nov 2022

Similar questions at other companies

  • Minimum Rounds to Complete All TasksInfosys · OAInfosys interview questions
  • Minimum-Sum Tree Fill with Unit StepsVisa · OAVisa interview questions
  • Server Upgrade PlanningWells Fargo · OAWells Fargo interview questions
  • Minimum N-ary Tree Depth DeletionsSnowflake · OA · PHONE SCREENSnowflake interview questions
  • Task Scheduler with CooldownGoDaddy · OA
  • Minimize Adjacent Squared-Difference Cost with One InsertionMcKinsey & Company · OAMcKinsey & Company interview questions
  • Maximize Revenue from Decreasing InventoryChubb · OAChubb interview questions
  • Minimize Adjacent Squared-Difference Cost with One InsertionMicrosoft · OAMicrosoft interview questions
  • Minimum Halvings for Alternating ParityIBM · OAIBM interview questions
  • Resource AllocationMaven Securities · OAMaven Securities interview questions

Interview reports that mention this question

  • Nutanix SWE Intern OA Questions: 16 Experiences (Part 2 of 3)

Nutanix interview experiences

  • Nutanix SWE Intern Phone Screens: 20 Experiences (Part 3 of 3)
  • Nutanix U.S. SWE Intern Interviews: 54 Experiences (Part 1 of 3)
  • Nutanix SWE OA and Interviews, Level Not Reported: 9 Experiences
  • Nutanix Intern Debugging Round and Interviews: 25 Experiences
All Nutanix interview questionsSee Nutanix hiring insights
Saved
class Solution {
    public int minKeyPresses(int n, int m) {
        // Write your code here.
    }
}
Checking account…