FastPrepAndroid Unlock Pattern Count

Android Unlock Pattern Count

Google logoGoogle● HardNEW GRADONSITE INTERVIEW
Learn

Problem statement

The Android unlock screen has nine keys in a 3x3 grid, numbered:

1 2 3
4 5 6
7 8 9

A pattern visits distinct keys. A move that passes directly through another key is allowed only if that intermediate key was already visited. For example, 1→3 requires 2, 1→9 requires 5, and 3→7 requires 5. All other moves are allowed.

Return the number of valid patterns whose length is between minLength and maxLength, inclusive.

Function

numberOfUnlockPatterns(minLength: int, maxLength: int) → int

Examples

Example 1

minLength = 1maxLength = 1return = 9

Every individual key is a valid length-one pattern.

Example 2

minLength = 1maxLength = 2return = 65

There are 9 length-one patterns and 56 valid length-two patterns.

Example 3

minLength = 4maxLength = 4return = 1624

The standard Android movement rules permit 1624 patterns of exactly four keys.

Constraints

  • 1 <= minLength <= maxLength <= 9.

More Google problems

See Google hiring insights
public int numberOfUnlockPatterns(int minLength, int maxLength) {
    // write your code here
}
minLength1
maxLength1
expected9
Checking account…