Prepin
Log in

Calculate the total number of possible combinations for N-digit numbers on a mobile keypad.

Approach / Explanation

This inquiry requires you to determine the total number of possible combinations for N-digit numbers that can be entered on a mobile keypad. The layout of the keypad is structured as follows: 1 2 3 4 5 6 7 8 9 0 For instance, when creating a two-digit number, you can move one step left, right, up, or down from each digit to select the next one. For example, if you are currently on '2', your next digit options could be '1', '5', or '3'. However, some digits restrict movement in certain directions; for instance, from '1', you cannot move left or up. To address this question, treat each button as a node in a graph, with edges connecting to other nodes that can be accessed from that button. This problem can be approached using dynamic programming by creating a lookup table and working from smaller values of N to larger ones. You can also apply an iterative breadth-first search (BFS) starting from each digit, treating each digit as a graph node. The BFS process for n digits from the current digit will yield the total count of n-length numbers that can be formed starting from that digit.

Suggested Answer

The total number of possible combinations can be determined by viewing each digit on the keypad as a node in a graph. We can apply dynamic programming to monitor the various number patterns that can be formed from each digit. For example, let's examine N-digit combinations for N=2. We begin with each digit individually (ranging from 0 to 9) and compute count(i, N) for all combinations of i with the other digits. This involves summing the counts of reachable nodes from i. We then add these counts and store the results before moving on to the next digit. By performing this process for every digit and aggregating all counts, we find the total possible combinations of 2-digit numbers, which can total 36.

Alternative Answer

Another analogous method that utilizes dynamic programming is as follows. Let’s consider a number N representing the digits. We will establish a lookup table initialized with a count of one for each digit from 0 to 9. If N=1, the outcome will be the total of all count values in the lookup table. For N >= 2, we will adjust the count for each digit based on its neighboring digits and continue this process for N iterations. After each iteration, the lookup table will be refreshed with new count values. The aggregated count values following N iterations will represent the total number of possible combinations. For instance, if N=2, after the first update, our lookup table will have the counts adjusted as follows: 0 - 1, 1 - 3 (1 can move to 2, 4, and itself), 2 - 4 (2 can move to 3, 1, 5, and itself), and so forth.. After the second iteration, our lookup table will be further updated to: 0 - 2 (from 1 and itself), 1 - 4 (from 2, 3, 5, and itself), 2 - 7 (from 1, 0, 3, 4, 5, 6, and itself) The total number of possible combinations will be the sum of the values in the lookup table after 2 iterations.

Question Details

Difficulty & Category

Medium

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.