Knight Dialer Sequences
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Bloomberg OA reported in November 2022 hides its trap in the keypad itself. Digit 5 has no knight moves, and 0 sits alone on the bottom row, so a lazy adjacency map or a missed edge gives you wrong counts that still look plausible. This is Knight Dialer Sequences: count length-n sequences of knight moves on a phone pad, modulo 1000000007. It's a dynamic programming problem with a tiny graph. If you blank on the transitions during the live assessment, StealthCoder is the safety net that runs invisibly and hands you the solution.
The problem
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 = 1 return = 10 Every one-digit sequence is valid. Example 2 n = 2 return = 20 There are twenty directed knight moves between keypad digits. Example 3 n = 3 return = 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.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to stop thinking about sequences and think about counts per ending digit. Keep an array of ten values where dp[d] is the number of valid sequences of the current length ending on d. For length 1, every entry is 1. Each step, the new dp[d] is the sum of old dp[s] for every digit s that can move to d. Hardcode the neighbors: 0:[4,6], 1:[6,8], 2:[7,9], 3:[4,8], 4:[0,3,9], 5:[], 6:[0,1,7], 7:[2,6], 8:[1,3], 9:[2,4]. Run n-1 steps, sum all ten, and take the mod. The pitfalls are forgetting the mod at each addition, and botching 5, which must contribute zero after length 1. Check your map against the examples: n=2 gives 20 and n=3 gives 46. If the numbers drift, the adjacency list is wrong. With n up to 5000, O(n * 10) is plenty. If you freeze under the clock, StealthCoder is the hedge that reads the problem and gives you this transition table.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Knight Dialer Sequences cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as knight dialer. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Knight Dialer Sequences FAQ
What's the trick in Knight Dialer Sequences?+
Track counts by ending digit instead of building sequences. Each step, a digit's new count is the sum of the counts of digits that can knight-move into it. Ten integers, n-1 iterations, mod at every addition. That collapses an exponential problem into linear time.
How hard is this Bloomberg OA question really?+
Medium. The idea is simple once you see the DP over ending digits. Most people lose points on the adjacency list, especially digit 5 having no moves and 0 connecting only to 4 and 6. Verify against the n=2 and n=3 examples.
Which adjacency list should I use?+
0:[4,6], 1:[6,8], 2:[7,9], 3:[4,8], 4:[0,3,9], 5:[], 6:[0,1,7], 7:[2,6], 8:[1,3], 9:[2,4]. The total number of moves is 20, which matches the n=2 answer. If your list sums to something else, fix it before writing code.
Do I need to worry about overflow or the modulo?+
Yes. Apply mod 1000000007 after each sum when building the new dp array, not just at the end. In Python it won't overflow, but the answer must still be reduced. In Java or C++, use long for the intermediate sums.
How do I prepare for this in 48 hours?+
Write the ten-digit DP once from scratch and confirm it returns 10, 20, and 46 for n=1, 2, 3. Then reduce space to two arrays. Also glance at similar grid-walk counting problems, since the same state-by-position DP shows up often.