Maximize the A's on screen with n presses of a four-key keyboard

Quick Overview

Find the largest number of A's a four-key keyboard (type A, select all, copy, paste) can put on the screen with at most n key presses, for n up to 50. It tests setting up a dynamic programming recurrence over press counts and reasoning about when a copy and paste run pays off.

Maximize the A's on screen with n presses of a four-key keyboard

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Imagine you have a special keyboard with the following four keys: - Key 1 (`A`): print one `'A'` on the screen. - Key 2 (`Ctrl-A`): select the whole screen. - Key 3 (`Ctrl-C`): copy the selection to the buffer. - Key 4 (`Ctrl-V`): print the buffer on the screen, appending it after what has already been printed. You can press the keyboard at most `n` times, using only these four keys. Return the maximum number of `'A'` characters you can print on the screen. ### Function Signature ```python def max_a_count(n: int) -> int: ``` ### Rules - The screen and the buffer start empty. - `Ctrl-C` replaces the buffer with the selected text. `Ctrl-V` appends the buffer to the end of the screen and keeps the existing text, even when that text is selected. - Pressing fewer than `n` keys is allowed, but it never helps, because one more `A` always adds a character. ### Constraints - `1 <= n <= 50` ### Examples ```text Input: n = 3 Output: 3 ``` A copy and a paste need three presses on top of at least one `A`, so with three presses the best you can do is press `A` three times. ```text Input: n = 7 Output: 9 ``` Press `A` three times, then `Ctrl-A`, `Ctrl-C`, `Ctrl-V`, `Ctrl-V`. The copy puts 3 A's in the buffer and each paste adds 3 more, for 9. Seven plain `A` presses give 7, and `A`, `A`, `Ctrl-A`, `Ctrl-C` followed by three pastes gives 8. ```text Input: n = 11 Output: 27 ``` The sequence from the previous example reaches 9 after seven presses. Then `Ctrl-A`, `Ctrl-C` and two pastes triple it to 27. Five `A` presses followed by `Ctrl-A`, `Ctrl-C` and four pastes gives only 25.

Overview: Find the largest number of A's a four-key keyboard (type A, select all, copy, paste) can put on the screen with at most n key presses, for n up to 50. It tests setting up a dynamic programming recurrence over press counts and reasoning about when a copy and paste run pays off.

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

Imagine you have a special keyboard with the following four keys:

  • Key 1 ( A ): print one 'A' on the screen.
  • Key 2 ( Ctrl-A ): select the whole screen.
  • Key 3 ( Ctrl-C ): copy the selection to the buffer.
  • Key 4 ( Ctrl-V ): print the buffer on the screen, appending it after what has already been printed.

You can press the keyboard at most n times, using only these four keys. Return the maximum number of 'A' characters you can print on the screen.

Function Signature

def max_a_count(n: int) -> int:

Rules

  • The screen and the buffer start empty.
  • Ctrl-C replaces the buffer with the selected text. Ctrl-V appends the buffer to the end of the screen and keeps the existing text, even when that text is selected.
  • Pressing fewer than n keys is allowed, but it never helps, because one more A always adds a character.

Constraints

  • 1 <= n <= 50

Examples

Input: n = 3
Output: 3

A copy and a paste need three presses on top of at least one A, so with three presses the best you can do is press A three times.

Input: n = 7
Output: 9

Press A three times, then Ctrl-A, Ctrl-C, Ctrl-V, Ctrl-V. The copy puts 3 A's in the buffer and each paste adds 3 more, for 9. Seven plain A presses give 7, and A, A, Ctrl-A, Ctrl-C followed by three pastes gives 8.

Input: n = 11
Output: 27

The sequence from the previous example reaches 9 after seven presses. Then Ctrl-A, Ctrl-C and two pastes triple it to 27. Five A presses followed by Ctrl-A, Ctrl-C and four pastes gives only 25.