Knight Dialer Sequences
Updated
Problem statement
A chess knight stands on a phone keypad laid out like this:
1 2 3
4 5 6
7 8 9
0 The knight begins on the key startDigit, which counts as pressed. It then makes exactly hops moves. Each move is a normal knight move: two keys in one direction (up, down, left or right) and one key to the side. A move must land on a digit key. The empty spots to the left and right of 0 are not keys. Each landing presses that key.
Return how many distinct digit sequences the knight can dial. Each sequence has hops + 1 digits and starts with startDigit. The knight may land on the same key more than once.
Function
countDialedNumbers(startDigit: int, hops: int) → longExamples
Example 1
startDigit = 1hops = 1return = 2From 1, the knight can only land on 6 or 8. The two sequences are 16 and 18.
Example 2
startDigit = 4hops = 2return = 6From 4, the first hop lands on 3, 9 or 0. Each of those has two next keys: 3 goes to 4 or 8, 9 goes to 2 or 4, and 0 goes to 4 or 6. That gives 3 * 2 = 6 sequences.
Example 3
startDigit = 5hops = 0return = 1With no hops, the only sequence is the starting key 5 by itself.
Example 4
startDigit = 5hops = 3return = 0No key is a knight move away from 5, so the knight cannot make even one hop. No sequence of the required length exists.
Constraints
0 <= startDigit <= 90 <= hops <= 40- The answer is less than
2^53, so it fits in a 64-bit integer.
More WhatNot Phone Screen questions
- Adjacent Pair and Run RemovalPHONE SCREEN · Seen Nov 2026
- Merge Sorted Squares of Two ArraysPHONE SCREEN · Seen Nov 2026
- User Journey PathsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Reachable Exits in an Obstacle GridPHONE SCREEN · Seen Oct 2026
- Palindrome with One DeletionPHONE SCREEN · Seen Oct 2026
- Search a 2D MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Attraction Route Without Reusing TrailsPHONE SCREEN · Seen Sep 2026
- Earliest Car for Each PassengerPHONE SCREEN · Seen Sep 2026
- Minimum Stores for a Shopping ListPHONE SCREEN · Seen Sep 2026
- User Journey Paths from a Log StreamPHONE SCREEN · Seen Jul 2026
Similar questions at other companies
- All Halfway Courses Across Course TracksCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Minimum Total Layover WaitGoogle · PHONE SCREENGoogle interview questions
- Shortest Paths in a Nonnegative Directed GraphNation with NaMo · PHONE SCREEN
- Minimum Traveling Salesperson TourGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
- Minimum Completion Time for DAG DependenciesAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Work Order Critical PathHadrian · PHONE SCREENHadrian interview questions
- Longest Name Chain in a Directed Acyclic GraphOscar Health · PHONE SCREEN
- Service Dependency Load FactorsRobinhood · PHONE SCREENRobinhood interview questions
- Employee Hierarchy Cycle and Depth AnalysisMicrosoft · PHONE SCREENMicrosoft interview questions
- Longest Chain BookingAirbnb · PHONE SCREENAirbnb interview questions