Count Valid Android Unlock Patterns Between Two Lengths

Last updated: November 29, 2026

Quick Overview

On a 3 by 3 Android lock screen grid, count unlock patterns that use between m and n distinct dots. A move may pass over a dot only if that dot has already been used.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


On a 3 by 3 Android lock screen grid, count unlock patterns that use between m and n distinct dots. A move may pass over a dot only if that dot has already been used.

A long-running Google interview favorite because it is literally about the Android lock screen. The interviewer checks that you encode the skip-over rule with a jump table and exploit the grid's symmetry to cut work by about four times.

What the Interviewer Expects
  • Builds a table of which dot lies between each pair of dots
  • Allows a jump only when the middle dot is already visited
  • Counts patterns of every length from m to n in one DFS
  • Uses symmetry: corners, edges, and center each computed once
  • Gives the bounded search size (at most 9! paths)
Key Topics to Cover
Backtracking
Grid symmetry
Jump table
Counting
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you generalize to a 4 by 4 grid where knight-like moves pass between dots?
  • How would you rank patterns by guessability?
  • Can you compute the answer for every length at once?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Dots numbered 1 to 9 as on a phone keypad.
  • Input: 1 <= m <= n <= 9.
  • Output: number of patterns with length in [m, n].
  • Rule: moving from a to b where a dot c lies exactly between them (for exam...
High-Level Design

Jump table skip[a][b] = the dot between a and b, or 0 if they are adjacent or a knight's move apart. There are only eight lines that pass a middle dot: rows (1-3 via 2, 4-6 via 5, 7-9 via 8), column...


Submit Your Answer
Markdown supported