← Whatnot Interview Insights

Whatnot·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

Whatnot SWE interview with a dynamic programming problem on knight moves across a phone keypad. Clean problem statement, but the modular arithmetic constraint and the 5000-hop upper bound meant a naive recursive approach was a non-starter.

Questions Asked (1)

Q1

Given a chess knight placed on a phone keypad, write a function that counts the number of distinct digit sequences the knight can dial starting from a given digit after exactly N hops. The keypad has two unusable corner cells, and the answer should be returned modulo 10^9+7.

Algorithms & Data Structures
Author's notes

My first instinct was recursion with memoization which felt fine until I noticed the hop count could go up to 5000.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the keypad as a graph where each digit is a node and knight moves define edges. Use dynamic programming to count sequences of length N+1 starting from the given digit, applying modulo 10^9+7 at each step. Optimize space by keeping only the previous hop's counts.

Pro tip: Clarify the keypad layout and knight move rules upfront, and mention that the two unusable corners are typically the bottom-left and bottom-right (digits * and #). Also, discuss potential optimizations like matrix exponentiation for very large N.

1. Clarify the problem

Confirm the keypad layout (usually 1-9, 0, with * and # as unusable corners) and that the knight starts on the given digit. Ensure you understand that a sequence of length N+1 includes the starting digit and N hops.

2. Define the graph

Map each digit to its valid knight-move neighbors on the keypad. Precompute this adjacency list to avoid recalculating moves during DP.

3. Design DP recurrence

Let dp[hop][digit] be the number of sequences of length hop+1 ending at digit. Initialize dp[0][start] = 1. For each hop, dp[hop][d] = sum(dp[hop-1][neighbor]) for all neighbors of d, modulo 10^9+7.

4. Optimize space

Since only the previous hop's counts are needed, use two arrays (or a single array updated in place with care) to reduce space complexity from O(N*10) to O(10).

5. Return result

After N hops, sum the counts for all digits (or just the count for the starting digit if the sequence must end at the start? Clarify: typically sum over all possible ending digits). Return the sum modulo 10^9+7.

Key Points to Mention

  • Graph representation of the keypad with knight moves.
  • Dynamic programming state definition and recurrence.
  • Modulo arithmetic to prevent overflow.
  • Space optimization using rolling arrays.
  • Time complexity O(N) and space O(1) (since keypad size is constant).
  • Handling of unusable corners (e.g., * and #) by excluding them from the graph.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.