FastPrepKnight Dialer Sequences
Problem · Dynamic Programming

Knight Dialer Sequences

Learn this problem
MediumConfluent logoConfluentFULLTIMEONSITE INTERVIEW

Problem statement

A chess knight is dialing numbers on the standard telephone keypad:

1 2 3
4 5 6
7 8 9
  0

A sequence may start on any digit. Each following digit must be reachable from the previous digit by one legal knight move. Given the sequence length n, return the number of valid digit sequences modulo 1000000007.

Function

countKnightDialerNumbers(n: int) → int

Examples

Example 1

n = 1return = 10

Every one-digit sequence is valid.

Example 2

n = 2return = 20

There are twenty directed knight moves between keypad digits.

Example 3

n = 3return = 46

Extending every valid two-digit sequence by one knight move produces forty-six sequences.

Constraints

  • 1 <= n <= 5000.
  • A sequence may begin at any of the ten digits.
  • Digit 5 has no legal outgoing knight move.

More Confluent problems

drafts saved locally
public int countKnightDialerNumbers(int n) {
    // Write your code here.
}
n1
expected10
checking account