Fewest moves from 1 to a target score with a limited number of doublings

Quick Overview

Starting from a score of 1, each move adds 1 to the score or doubles it, and only a limited number of doublings are allowed. The task is to find the fewest moves that reach a target of up to one billion. It tests greedy reasoning backward from the target and a careful argument about where the limited doubles should go.

Fewest moves from 1 to a target score with a limited number of doublings

Company: Wayfair

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are playing a game with integers. You start with a score of `1` and you want to reach a score of `target`. In one move, you can either: - increase your score by `1`, or - double your score (you can use this operation at most `max_doubles` times). Given the two integers `target` and `max_doubles`, return the minimum number of moves needed to reach `target` starting from a score of `1`. ### Function Signature ```python def min_moves(target: int, max_doubles: int) -> int: ``` ### Rules - Increments are unlimited. Doublings are limited to `max_doubles`, and you do not have to use all of them. - The score never decreases, so the sequence must land exactly on `target`. - When `target` is `1` the answer is `0`, whatever `max_doubles` is. ### Constraints - `1 <= target <= 10^9` - `0 <= max_doubles <= 100` ### Examples ```text Input: target = 5, max_doubles = 0 Output: 4 ``` With no doubles allowed, the only option is to increment 4 times: `1 -> 2 -> 3 -> 4 -> 5`. ```text Input: target = 19, max_doubles = 2 Output: 7 ``` One optimal sequence uses both doubles: three increments to 4, double to 8, increment to 9, double to 18, increment to 19. That is 3 + 1 + 1 + 1 + 1 = 7 moves. ```text Input: target = 10, max_doubles = 4 Output: 4 ``` The path `1 -> 2 -> 4 -> 5 -> 10` takes 4 moves and stays within the four allowed doubles. No 3-move path exists even with unlimited doubles, because three moves can only reach 4, 5, 6 or 8.

Overview: Starting from a score of 1, each move adds 1 to the score or doubles it, and only a limited number of doublings are allowed. The task is to find the fewest moves that reach a target of up to one billion. It tests greedy reasoning backward from the target and a careful argument about where the limited doubles should go.

Wayfair logo
Wayfair
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are playing a game with integers. You start with a score of 1 and you want to reach a score of target.

In one move, you can either:

  • increase your score by 1 , or
  • double your score (you can use this operation at most max_doubles times).

Given the two integers target and max_doubles, return the minimum number of moves needed to reach target starting from a score of 1.

Function Signature

def min_moves(target: int, max_doubles: int) -> int:

Rules

  • Increments are unlimited. Doublings are limited to max_doubles , and you do not have to use all of them.
  • The score never decreases, so the sequence must land exactly on target .
  • When target is 1 the answer is 0 , whatever max_doubles is.

Constraints

  • 1 <= target <= 10^9
  • 0 <= max_doubles <= 100

Examples

Input: target = 5, max_doubles = 0
Output: 4

With no doubles allowed, the only option is to increment 4 times: 1 -> 2 -> 3 -> 4 -> 5.

Input: target = 19, max_doubles = 2
Output: 7

One optimal sequence uses both doubles: three increments to 4, double to 8, increment to 9, double to 18, increment to 19. That is 3 + 1 + 1 + 1 + 1 = 7 moves.

Input: target = 10, max_doubles = 4
Output: 4

The path 1 -> 2 -> 4 -> 5 -> 10 takes 4 moves and stays within the four allowed doubles. No 3-move path exists even with unlimited doubles, because three moves can only reach 4, 5, 6 or 8.