KatInterview

972. Knight Dialer

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

Method: Dynamic Programming with Space Optimization

This problem asks for the number of distinct sequences of length n that can be formed by a knight's moves on a phone pad. This can be modeled as finding the total number of paths of length n-1 in a graph, where the nodes are the digits and edges represent valid knight moves. This structure is perfect for a dynamic programming solution.

Solution

Knight Dialer in Python

class Solution:
    def knightDialer(self, n: int) -> int:
        # MOD for the result
        MOD = 10**9 + 7

        # Special case: if n=1, we can start at any of the 10 digits.
        if n == 1:
            return 10

        # dp[i] = number of ways to end on digit i after some number of jumps.
        # Initialize for length 1 (0 jumps).
        dp = [1] * 10

        # We need to make n-1 jumps.
        for _ in range(n - 1):
            next_dp = [0] * 10
            # Calculate next state based on current state (dp)
            # Jumps to 0 come from 4, 6
            next_dp[0] = (dp[4] + dp[6]) % MOD
            # Jumps to 1 come from 6, 8
            next_dp[1] = (dp[6] + dp[8]) % MOD
            # Jumps to 2 come from 7, 9
            next_dp[2] = (dp[7] + dp[9]) % MOD
            # Jumps to 3 come from 4, 8
            next_dp[3] = (dp[4] + dp[8]) % MOD
            # Jumps to 4 come from 0, 3, 9
            next_dp[4] = (dp[0] + dp[3] + dp[9]) % MOD
            # Jumps to 5 are not possible.
            next_dp[5] = 0
            # Jumps to 6 come from 0, 1, 7
            next_dp[6] = (dp[0] + dp[1] + dp[7]) % MOD
            # Jumps to 7 come from 2, 6
            next_dp[7] = (dp[2] + dp[6]) % MOD
            # Jumps to 8 come from 1, 3
            next_dp[8] = (dp[1] + dp[3]) % MOD
            # Jumps to 9 come from 2, 4
            next_dp[9] = (dp[2] + dp[4]) % MOD

            # Move to the next state
            dp = next_dp

        # Sum up all the possibilities for length n
        return sum(dp) % MOD

Knight Dialer in Java

class Solution {
    public int knightDialer(int n) {
        // MOD for the result
        int MOD = 1_000_000_007;

        // Special case: if n=1, we can start at any of the 10 digits.
        if (n == 1) {
            return 10;
        }

        // dp[i] = number of ways to end on digit i after some number of jumps.
        // Initialize for length 1 (0 jumps). Use long to prevent overflow during additions.
        long[] dp = new long[10];
        java.util.Arrays.fill(dp, 1);

        // We need to make n-1 jumps.
        for (int i = 0; i < n - 1; i++) {
            long[] next_dp = new long[10];
            // Calculate next state based on current state (dp)
            // From 0, can jump to 4, 6
            next_dp[0] = (dp[4] + dp[6]) % MOD;
            // From 1, can jump to 6, 8
            next_dp[1] = (dp[6] + dp[8]) % MOD;
            // From 2, can jump to 7, 9
            next_dp[2] = (dp[7] + dp[9]) % MOD;
            // From 3, can jump to 4, 8
            next_dp[3] = (dp[4] + dp[8]) % MOD;
            // From 4, can jump to 0, 3, 9
            next_dp[4] = (dp[0] + dp[3] + dp[9]) % MOD;
            // From 5, no jumps are possible.
            next_dp[5] = 0;
            // From 6, can jump to 0, 1, 7
            next_dp[6] = (dp[0] + dp[1] + dp[7]) % MOD;
            // From 7, can jump to 2, 6
            next_dp[7] = (dp[2] + dp[6]) % MOD;
            // From 8, can jump to 1, 3
            next_dp[8] = (dp[1] + dp[3]) % MOD;
            // From 9, can jump to 2, 4
            next_dp[9] = (dp[2] + dp[4]) % MOD;

            // Move to the next state
            dp = next_dp;
        }

        // Sum up all the possibilities for length n
        long total = 0;
        for (long count : dp) {
            total = (total + count) % MOD;
        }

        return (int) total;
    }
}

Knight Dialer in C++

class Solution {
public:
    int knightDialer(int n) {
        // MOD for the result
        int MOD = 1e9 + 7;

        // Special case: if n=1, we can start at any of the 10 digits.
        if (n == 1) {
            return 10;
        }

        // dp[i] = number of ways to end on digit i after some number of jumps.
        // Initialize for length 1 (0 jumps).
        std::vector<long long> dp(10, 1);

        // We need to make n-1 jumps.
        for (int i = 0; i < n - 1; ++i) {
            std::vector<long long> next_dp(10, 0);
            // Calculate next state based on current state (dp)
            // From 0, can jump to 4, 6
            next_dp[0] = (dp[4] + dp[6]) % MOD;
            // From 1, can jump to 6, 8
            next_dp[1] = (dp[6] + dp[8]) % MOD;
            // From 2, can jump to 7, 9
            next_dp[2] = (dp[7] + dp[9]) % MOD;
            // From 3, can jump to 4, 8
            next_dp[3] = (dp[4] + dp[8]) % MOD;
            // From 4, can jump to 0, 3, 9
            next_dp[4] = (dp[0] + dp[3] + dp[9]) % MOD;
            // From 5, no jumps are possible.
            next_dp[5] = 0;
            // From 6, can jump to 0, 1, 7
            next_dp[6] = (dp[0] + dp[1] + dp[7]) % MOD;
            // From 7, can jump to 2, 6
            next_dp[7] = (dp[2] + dp[6]) % MOD;
            // From 8, can jump to 1, 3
            next_dp[8] = (dp[1] + dp[3]) % MOD;
            // From 9, can jump to 2, 4
            next_dp[9] = (dp[2] + dp[4]) % MOD;
            
            // Move to the next state
            dp = next_dp;
        }

        // Sum up all the possibilities for length n
        long long total = 0;
        for (long long count : dp) {
            total = (total + count) % MOD;
        }

        return static_cast<int>(total);
    }
};

Knight Dialer in TypeScript

function knightDialer(n: number): number {
    // MOD for the result
    const MOD = 10**9 + 7;

    // Special case: if n=1, we can start at any of the 10 digits.
    if (n === 1) {
        return 10;
    }

    // dp[i] = number of ways to end on digit i after some number of jumps.
    // Initialize for length 1 (0 jumps).
    let dp: number[] = new Array(10).fill(1);

    // We need to make n-1 jumps.
    for (let i = 0; i < n - 1; i++) {
        let next_dp: number[] = new Array(10).fill(0);
        // Calculate next state based on current state (dp)
        // From 0, can jump to 4, 6
        next_dp[0] = (dp[4] + dp[6]) % MOD;
        // From 1, can jump to 6, 8
        next_dp[1] = (dp[6] + dp[8]) % MOD;
        // From 2, can jump to 7, 9
        next_dp[2] = (dp[7] + dp[9]) % MOD;
        // From 3, can jump to 4, 8
        next_dp[3] = (dp[4] + dp[8]) % MOD;
        // From 4, can jump to 0, 3, 9
        next_dp[4] = (dp[0] + dp[3] + dp[9]) % MOD;
        // From 5, no jumps are possible.
        next_dp[5] = 0;
        // From 6, can jump to 0, 1, 7
        next_dp[6] = (dp[0] + dp[1] + dp[7]) % MOD;
        // From 7, can jump to 2, 6
        next_dp[7] = (dp[2] + dp[6]) % MOD;
        // From 8, can jump to 1, 3
        next_dp[8] = (dp[1] + dp[3]) % MOD;
        // From 9, can jump to 2, 4
        next_dp[9] = (dp[2] + dp[4]) % MOD;

        // Move to the next state
        dp = next_dp;
    }

    // Sum up all the possibilities for length n
    let total = 0;
    for (const count of dp) {
        total = (total + count) % MOD;
    }

    return total;
}

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.