Fewest Button Presses to Reach N Using Plus One and Double

Last updated: November 29, 2026

Quick Overview

A counter starts at 1 and a remote has two buttons: add one, or double the current value. Return the fewest presses needed to make the counter show exactly n.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Easy

0

0

0 solved


A counter starts at 1 and a remote has two buttons: add one, or double the current value. Return the fewest presses needed to make the counter show exactly n.

Google uses small number-puzzle DPs like this in early technical screens to see whether you think about which earlier states feed a given state. Strong candidates also notice the binary-representation shortcut and can explain why both agree.

What the Interviewer Expects
  • Define presses[i] for every value up to n
  • Identify predecessors: i - 1 always, and i / 2 when i is even
  • Fill the table bottom up from 1
  • Connect the result to the binary form of n
  • Discuss the greedy reverse process as a check
Key Topics to Cover
1D dynamic programming
Shortest path on integers
Binary representation
Greedy verification
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Add a third button that triples the value.
  • What if the counter can also subtract one?
  • Return the actual button sequence.
  • Solve it for n up to 10^18 without a table.
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: integer n >= 1. The counter starts at 1. Buttons: +1 and x2. Output the minimum number of presses to reach n exactly. Since values only increase, every path from 1 to n stays within 1..n....

High-Level Design

Let presses[i] be the minimum presses to reach i. The last press was either +1 from i - 1 or x2 from i / 2 if i is even. So presses[i] = presses[i-1] + 1, improved to presses[i//2] + 1 when ...


Submit Your Answer
Markdown supported