Minimum Button Presses to Set a Microwave Timer

Quick Overview

Given a cooking time in seconds, find the fewest button presses needed to enter it on a microwave keypad where the last two digits are seconds and may exceed 59. The problem tests careful case analysis of an input encoding and its boundary values.

Minimum Button Presses to Set a Microwave Timer

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

A microwave has a numeric keypad (digits `0` to `9`) and a start button. The timer display shows `MM:SS` format. You want to heat food for a specific number of seconds. You can enter the time in different ways, since the microwave interprets the input based on the number of digits entered: - 1 digit `D`: interpreted as `D` seconds. - 2 digits `AB`: interpreted as `AB` seconds. - 3 digits `ABC`: interpreted as `A` minutes and `BC` seconds. - 4 digits `ABCD`: interpreted as `AB` minutes and `CD` seconds. The seconds portion can exceed 59. For example, entering `90` is valid and means 1 minute 30 seconds. Given a `target` number of seconds, return the minimum number of button presses (digit presses plus 1 for the start button) needed to set the microwave for exactly `target` seconds. ### Function Signature ```python def min_button_presses(target: int) -> int: ``` ### Rules - An entry is a string of 1 to 4 digits followed by the start button. Leading zeros are allowed (`05` means 5 seconds, `0500` means 5 minutes 0 seconds). - In a 3-digit or 4-digit entry, the last two digits are the seconds (`00` to `99`) and the digits before them are the minutes. The entry sets `minutes * 60 + seconds` seconds, which must equal `target`. - The start button is always pressed once and counts as 1 press. ### Constraints - `1 <= target <= 5999` - `target` is given in total seconds. ### Examples ```text Input: target = 90 Output: 3 ``` `90` (two digits, 90 seconds) plus start is 3 presses. The entry `130` (1 minute 30 seconds) also works but takes 4 presses. ```text Input: target = 300 Output: 4 ``` 300 seconds is 5 minutes, which can be entered as `500` or as `460` (4 minutes 60 seconds), each 4 presses with start. No 1-digit or 2-digit entry reaches 300, and `0500` is valid but takes 5 presses. ```text Input: target = 650 Output: 5 ``` The largest 3-digit entry is `999`, which is 9 minutes 99 seconds, or 639 seconds. So 650 needs 4 digits: `1050` (10 minutes 50 seconds) plus start is 5 presses.

Overview: Given a cooking time in seconds, find the fewest button presses needed to enter it on a microwave keypad where the last two digits are seconds and may exceed 59. The problem tests careful case analysis of an input encoding and its boundary values.

Google logo
Google
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

A microwave has a numeric keypad (digits 0 to 9) and a start button. The timer display shows MM:SS format. You want to heat food for a specific number of seconds.

You can enter the time in different ways, since the microwave interprets the input based on the number of digits entered:

  • 1 digit D : interpreted as D seconds.
  • 2 digits AB : interpreted as AB seconds.
  • 3 digits ABC : interpreted as A minutes and BC seconds.
  • 4 digits ABCD : interpreted as AB minutes and CD seconds.

The seconds portion can exceed 59. For example, entering 90 is valid and means 1 minute 30 seconds.

Given a target number of seconds, return the minimum number of button presses (digit presses plus 1 for the start button) needed to set the microwave for exactly target seconds.

Function Signature

def min_button_presses(target: int) -> int:

Rules

  • An entry is a string of 1 to 4 digits followed by the start button. Leading zeros are allowed ( 05 means 5 seconds, 0500 means 5 minutes 0 seconds).
  • In a 3-digit or 4-digit entry, the last two digits are the seconds ( 00 to 99 ) and the digits before them are the minutes. The entry sets minutes * 60 + seconds seconds, which must equal target .
  • The start button is always pressed once and counts as 1 press.

Constraints

  • 1 <= target <= 5999
  • target is given in total seconds.

Examples

Input: target = 90
Output: 3

90 (two digits, 90 seconds) plus start is 3 presses. The entry 130 (1 minute 30 seconds) also works but takes 4 presses.

Input: target = 300
Output: 4

300 seconds is 5 minutes, which can be entered as 500 or as 460 (4 minutes 60 seconds), each 4 presses with start. No 1-digit or 2-digit entry reaches 300, and 0500 is valid but takes 5 presses.

Input: target = 650
Output: 5

The largest 3-digit entry is 999, which is 9 minutes 99 seconds, or 639 seconds. So 650 needs 4 digits: 1050 (10 minutes 50 seconds) plus start is 5 presses.