Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

935. Knight Dialer

Medium60.8% Acceptance
Dynamic Programming
Asked by:
B
Bloomberg
F
Facebook
ProblemSolutions (6)VideosCompanies (6)Notes

Problem Statement

The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagram:

A chess knight can move as indicated in the chess diagram below:

We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).

Given an integer n, return how many distinct phone numbers of length n we can dial.

You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.

As the answer may be very large, return the answer modulo 109 + 7.

Example 1:

Input: n = 1
Output: 10
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.

Example 2:

Input: n = 2
Output: 20
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]

Example 3:

Input: n = 3131
Output: 136006598
Explanation: Please take care of the mod.

Constraints:

  • 1 <= n <= 5000
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
A
Amazon
T
Twilio
M
Microsoft
+1

Approach

In #935 Knight Dialer, you must count how many distinct phone numbers of length n can be formed by moving a chess knight on a numeric keypad. The key observation is that each digit can only move to a fixed set of digits according to knight movement rules. This makes the problem well-suited for dynamic programming.

Define a DP state where dp[i][d] represents the number of ways to end on digit d after i moves. For each step, distribute counts to all digits reachable via valid knight moves. Since the keypad graph is small (digits 0–9), transitions are constant and can be precomputed.

You can optimize space by storing only the previous step, reducing memory to O(10). All results should be computed modulo 1e9+7 to avoid overflow. The dynamic programming approach runs in O(n) time with a constant-sized state. For very large n, the problem can also be modeled as transitions in a graph and solved using matrix exponentiation.

Complexity

ApproachTime ComplexitySpace Complexity
Dynamic Programming with Rolling ArrayO(n)O(10)
DP with Full TableO(n × 10)O(n × 10)
Matrix ExponentiationO(10^3 log n)O(10^2)

Video Solution Available

NeetCodeIO

View all video solutions

Solutions (6)

Dynamic Programming Approach

This approach uses dynamic programming to store and reuse the number of dialable numbers of length n starting from each digit. We maintain a 2D DP table where dp[i][j] indicates how many numbers of length i can start from digit j.

The primary idea is to iterate from i=1 to n, updating the DP table based on the moves possible from each digit in the previous state.

Time Complexity: O(n) for each of the 10 digits, resulting in O(10n) operations.
Space Complexity: O(10n) due to the DP table.

PythonJavaC++
1def knightDialer(n):
2    MOD = 10**9 + 7
3    moves = {0: [4, 6], 1: [6, 8], 2: [7, 9], 3: [4, 8],
4             4: [0, 3, 9], 5: [], 6: [0, 1, 7], 7: [2, 6],
5             8: [1, 3], 9: [2, 4]}
6             
7    dp = [[0] * 10 for _ in range(n)]
8    for i in range(10):
9        dp[0][i] = 1
10    
11    for i in range(1, n):
12        for j in range(10):
13            dp[i][j] = sum(dp[i - 1][m] for m in moves[j]) % MOD
14
15    return sum(dp[n - 1]) % MOD
16

Explanation

This function uses dynamic programming to calculate the number of valid phone numbers. The moves are stored in a dictionary with each digit pointing to its possible knight moves. We initialize a 2D list dp where dp[i][j] represents the number of numbers of length i+1 starting from digit j. The result is obtained by summing up the values in the last row of the dp table.

Optimized Dynamic Programming with Space Reduction

This approach optimizes the dynamic programming technique by reducing space complexity. Instead of using a full 2D array, we use two 1D arrays to track only the current and the previous state, thus reducing the space required from O(n*10) to O(20).

Time Complexity: O(n) - iterating over digits with constant time calculations;
Space Complexity: O(10) as we only hold current and previous state arrays.

PythonJavaC++
1def knightDialer(n):
2

Video Solutions

Watch expert explanations and walkthroughs

Knight Dialer - Leetcode 935 - Python

NeetCodeIO
16:3910,814 views

Asked By Companies

6 companies
B
Bloomberg
F
Facebook
A
Amazon
T
Twilio
M
Microsoft
S
Snapchat

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Longest Palindromic SubstringMedium
Regular Expression MatchingHard
Generate ParenthesesMedium
Longest Valid ParenthesesHard
More similar problems

Related Topics

Dynamic Programming

Problem Stats

Acceptance Rate60.8%
DifficultyMedium
Companies6

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Knight Dialer asked in FAANG interviews?

Yes, Knight Dialer is a popular dynamic programming problem often used in technical interviews. It tests graph thinking, state transitions, and DP optimization, which are common patterns in FAANG-style interviews.

Why do we use modulo 1e9+7 in Knight Dialer?

The number of possible sequences grows exponentially as n increases. Using modulo 1e9+7 prevents integer overflow and keeps results within manageable limits while preserving correctness in modular arithmetic.

What data structure is best for Knight Dialer?

A small DP array of size 10 is sufficient because the keypad only contains digits 0–9. Additionally, a predefined adjacency list representing valid knight moves helps efficiently update transitions.

What is the optimal approach for Knight Dialer?

The optimal approach uses dynamic programming by tracking the number of ways to reach each digit at every step. Since each digit has fixed knight transitions, you repeatedly update counts for n moves. Using a rolling array reduces space to constant size.

MOD
=
10
**
9
+
7
3
moves
=
{
0
:
[
4
,
6
]
,
1
:
[
6
,
8
]
,
2
:
[
7
,
9
]
,
3
:
[
4
,
8
]
,
4
4
:
[
0
,
3
,
9
]
,
5
:
[
]
,
6
:
[
0
,
1
,
7
]
,
7
:
[
2
,
6
]
,
5
8
:
[
1
,
3
]
,
9
:
[
2
,
4
]
}
6
7
prev
=
[
1
]
*
10
8
for
_
in
range
(
1
,
n
)
:
9
curr
=
[
0
]
*
10
10
for
j
in
range
(
10
)
:
11
curr
[
j
]
=
sum
(
prev
[
m
]
for
m
in
moves
[
j
]
)
%
MOD
12
prev
=
curr
13
14
return
sum
(
prev
)
%
MOD
15

Explanation

This function uses a space-efficient dynamic programming approach. Instead of maintaining a 2D table, it uses two lists: prev and curr, which track the current and previous counts of numbers of length n. The prev list is updated at each step to effectively simulate the transition from i to i+1 without maintaining an entire history.