Skip to main content

Android Unlock Patterns - Solution & Explanation

MediumPremiumFree on FleetCodeDynamic ProgrammingBacktrackingBit ManipulationBitmask8 min readAsked at: Google
Practice this problem

Problem Statement

Android devices have a special lock screen with a 3 x 3 grid of dots. Users can set an "unlock pattern" by connecting the dots in a specific sequence, forming a series of joined line segments where each segment's endpoints are two consecutive dots in the sequence. A sequence of k dots is a valid unlock pattern if both of the following are true:

  • All the dots in the sequence are distinct.
  • If the line segment connecting two consecutive dots in the sequence passes through the center of any other dot, the other dot must have previously appeared in the sequence. No jumps through the center non-selected dots are allowed.
    • For example, connecting dots 2 and 9 without dots 5 or 6 appearing beforehand is valid because the line from dot 2 to dot 9 does not pass through the center of either dot 5 or 6.
    • However, connecting dots 1 and 3 without dot 2 appearing beforehand is invalid because the line from dot 1 to dot 3 passes through the center of dot 2.

Here are some example valid and invalid unlock patterns:

  • The 1st pattern [4,1,3,6] is invalid because the line connecting dots 1 and 3 pass through dot 2, but dot 2 did not previously appear in the sequence.
  • The 2nd pattern [4,1,9,2] is invalid because the line connecting dots 1 and 9 pass through dot 5, but dot 5 did not previously appear in the sequence.
  • The 3rd pattern [2,4,1,3,6] is valid because it follows the conditions. The line connecting dots 1 and 3 meets the condition because dot 2 previously appeared in the sequence.
  • The 4th pattern [6,5,4,1,9,2] is valid because it follows the conditions. The line connecting dots 1 and 9 meets the condition because dot 5 previously appeared in the sequence.

Given two integers m and n, return the number of unique and valid unlock patterns of the Android grid lock screen that consist of at least m keys and at most n keys.

Two unlock patterns are considered unique if there is a dot in one sequence that is not in the other, or the order of the dots is different.

 

Example 1:

Input: m = 1, n = 1
Output: 9

Example 2:

Input: m = 1, n = 2
Output: 65

 

Constraints:

  • 1 <= m, n <= 9

Approach Overview

Problem Overview: Count how many valid unlock patterns can be formed on the Android 3x3 lock screen using lengths between m and n. A pattern cannot revisit a key, and moves that jump over another key are only allowed if that intermediate key has already been used.

Approach 1: Brute Force Backtracking (O(9!))

The straightforward strategy generates every possible path on the 3×3 grid using depth‑first search. Start from each digit (1–9), mark it visited, and recursively try the remaining digits while respecting the Android rule: if a move crosses another key (for example 1 → 3 crossing 2), that intermediate key must already be visited. A small lookup table called skip stores these required intermediate nodes. The search continues until the path length reaches n, counting patterns whose length is at least m. Time complexity is O(9!) in the worst case because permutations of keys are explored, and space complexity is O(9) for the recursion stack and visited array.

Approach 2: Symmetry Optimized Backtracking (O(9!))

The grid has strong symmetry. Corners (1,3,7,9) behave the same, edges (2,4,6,8) behave the same, and the center (5) is unique. Instead of running DFS from all nine digits, run it once from a representative of each group and multiply the result. For example, compute patterns starting from 1 and multiply by four for the corners. Do the same for 2 multiplied by four for edges, and handle 5 once. The same skip matrix enforces the rule about crossing intermediate keys. This reduces redundant exploration while keeping the same theoretical complexity O(9!), with space O(9). This approach is what most production solutions use because it cuts the constant factor dramatically.

Approach 3: Bitmask + DP State Exploration (O(9 * 2^9))

A more algorithmic perspective models the problem as state transitions. Each state is defined by the current key and a bitmask representing visited keys. From a state (node, mask), iterate over all possible next digits and check whether the move is valid using the same intermediate-key rule. Memoizing results for identical states avoids recomputation. The number of states is bounded by 9 * 2^9, giving time complexity around O(9 * 2^9) and space complexity O(9 * 2^9). This version connects naturally to dynamic programming and bitmask techniques.

Recommended for interviews: Symmetry‑optimized backtracking is the expected solution. It demonstrates control of DFS, constraint handling, and pruning using problem structure. Interviewers like seeing the skip matrix plus the symmetry observation because it reduces work from nine starting searches to three. Mentioning the bitmask state idea also shows familiarity with backtracking and DP tradeoffs.

Solution

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force BacktrackingO(9!)O(9)Understanding the full search space and Android movement rules
Symmetry Optimized BacktrackingO(9!)O(9)Best practical solution; reduces repeated DFS using grid symmetry
Bitmask Dynamic ProgrammingO(9 * 2^9)O(9 * 2^9)When modeling states formally or demonstrating DP with bitmasking

Video Solution

LeetCode 351. Android Unlock PatternsHappy Coding6,430 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Android Unlock Patterns easy or hard?
Android Unlock Patterns is typically classified as Medium difficulty. The backtracking itself is straightforward, but the tricky part is encoding the intermediate key rule and recognizing the symmetry optimization to reduce redundant searches.
Android Unlock Patterns Python/Java solution
Implement a DFS function that tracks the current key, visited set, and remaining length. Use a skip matrix to validate moves such as 1→3 requiring 2 or 1→9 requiring 5. Run DFS from representative digits (1, 2, and 5) and multiply counts using symmetry. The same logic translates easily across Python, Java, C++, Go, and TypeScript.
How to solve Android Unlock Patterns in O(n)?
An exact O(n) solution does not exist because the algorithm must explore combinations of keys that form valid patterns. The closest efficient formulation uses backtracking with pruning or a DP state defined by (current key, visited bitmask). That DP approach runs in roughly O(9 * 2^9) states.
What is the best approach for Android Unlock Patterns?
Symmetry optimized backtracking is the most common solution. Use DFS with a visited array and a skip matrix that stores which intermediate key must be visited for certain moves. Start from three representative keys (corner, edge, center) and multiply results using grid symmetry. This keeps the logic simple while reducing redundant searches.
Is Android Unlock Patterns asked at Google/Amazon/Meta?
Android Unlock Patterns has appeared in interviews at companies that emphasize recursion and state search problems. Variations of the question have been reported in Google and other large tech company interviews because it tests backtracking, constraint validation, and optimization through symmetry.
What data structure is used in Android Unlock Patterns?
The solution typically uses a visited array or bitmask to track selected keys and a 10x10 skip matrix that records intermediate keys required between two digits. DFS recursion explores valid next moves while enforcing the skip rule.
What is the time complexity of Android Unlock Patterns?
The worst case time complexity is O(9!) because the algorithm explores permutations of the nine keys with pruning rules. In practice the search space is much smaller due to the Android movement constraints and symmetry optimization. Space complexity is O(9) for the recursion stack and visited tracking.

Ready to solve this problem?

Practice Android Unlock Patterns with our built-in code editor and test cases.

Practice on FleetCode