Minimum remote presses to reach a TV channel

Quick Overview

Find the fewest remote presses that move a TV from one channel to another when Up and Down wrap around, Back returns to the last channel, and digit keys jump to any channel. It tests shortest-path modeling, wrap-around arithmetic, and reasoning about which button combinations can be optimal.

Minimum remote presses to reach a TV channel

Company: Thoughtworks

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

A TV has `n` channels numbered from `1` to `n`. You are on channel `current` and want to reach channel `target`. Return the minimum number of button presses on the remote control that gets you there. The remote has these buttons, and every press costs 1: - Up goes to the next higher channel. It wraps from channel `n` to channel `1`. - Down goes to the next lower channel. It wraps from channel `1` to channel `n`. - Back goes to the previously visited channel. It is unavailable until you have changed channel at least once. - The number keys `0` to `9` type a channel number. Each digit is one press, so typing channel `123` costs 3 presses and jumps straight to channel 123. ### Function Signature ```python def min_presses(n: int, current: int, target: int) -> int: ``` ### Rules - You may type any channel number from `1` to `n`, not only `target`, and keep pressing Up, Down or Back afterwards. - Typing channel `x` costs as many presses as `x` has digits. Numbers are typed without leading zeros, and only numbers from `1` to `n` can be typed. - If `current == target`, the answer is 0. ### Constraints - `1 <= n <= 10000` - `1 <= current <= n` - `1 <= target <= n` ### Examples ```text Input: n = 100, current = 15, target = 20 Output: 2 ``` Pressing Up five times costs 5 presses, while typing `2` and `0` costs 2. No single press reaches channel 20 from channel 15, so 2 is the minimum. ```text Input: n = 500, current = 1, target = 500 Output: 1 ``` Down wraps from channel 1 to channel 500 in one press, which beats typing `500` (3 presses). ```text Input: n = 1000, current = 500, target = 1000 Output: 2 ``` Typing `1` jumps to channel 1, and Down then wraps to channel 1000, for 2 presses in total. Typing `1000` costs 4, walking costs 500, and no single press reaches channel 1000.

Overview: Find the fewest remote presses that move a TV from one channel to another when Up and Down wrap around, Back returns to the last channel, and digit keys jump to any channel. It tests shortest-path modeling, wrap-around arithmetic, and reasoning about which button combinations can be optimal.

Thoughtworks logo
Thoughtworks
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

A TV has n channels numbered from 1 to n. You are on channel current and want to reach channel target. Return the minimum number of button presses on the remote control that gets you there.

The remote has these buttons, and every press costs 1:

  • Up goes to the next higher channel. It wraps from channel n to channel 1 .
  • Down goes to the next lower channel. It wraps from channel 1 to channel n .
  • Back goes to the previously visited channel. It is unavailable until you have changed channel at least once.
  • The number keys 0 to 9 type a channel number. Each digit is one press, so typing channel 123 costs 3 presses and jumps straight to channel 123.

Function Signature

def min_presses(n: int, current: int, target: int) -> int:

Rules

  • You may type any channel number from 1 to n , not only target , and keep pressing Up, Down or Back afterwards.
  • Typing channel x costs as many presses as x has digits. Numbers are typed without leading zeros, and only numbers from 1 to n can be typed.
  • If current == target , the answer is 0.

Constraints

  • 1 <= n <= 10000
  • 1 <= current <= n
  • 1 <= target <= n

Examples

Input: n = 100, current = 15, target = 20
Output: 2

Pressing Up five times costs 5 presses, while typing 2 and 0 costs 2. No single press reaches channel 20 from channel 15, so 2 is the minimum.

Input: n = 500, current = 1, target = 500
Output: 1

Down wraps from channel 1 to channel 500 in one press, which beats typing 500 (3 presses).

Input: n = 1000, current = 500, target = 1000
Output: 2

Typing 1 jumps to channel 1, and Down then wraps to channel 1000, for 2 presses in total. Typing 1000 costs 4, walking costs 500, and no single press reaches channel 1000.