FastPrepMinimum Steps with Double and Floor-Divide

Minimum Steps with Double and Floor-Divide

Bloomberg LP logoBloomberg LP● MediumNEW GRADPHONE SCREENONSITE INTERVIEW
Learn

Problem statement

Start with the integer 1. In one step, replace the current value x with either:

  • x * 2, or
  • floor(x / 3).

Given target, return the minimum number of steps needed to reach it.

Function

minStepsToTarget(target: int) → int

Examples

Example 1

target = 1return = 0

The starting value already equals the target.

Example 2

target = 5return = 5

One shortest sequence is 1 -> 2 -> 4 -> 8 -> 16 -> 5.

Example 3

target = 3return = 7

A shortest sequence is 1 -> 2 -> 4 -> 8 -> 16 -> 32 -> 10 -> 3.

Constraints

  • 0 <= target <= 100
  • All intermediate values are represented with signed 64-bit integers.
  • Every target in the allowed range is reachable.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public int minStepsToTarget(int target) {
    // Write your code here.
}
target1
expected0
Checking account…