Code Room
CodingHardcod-g797
Subject BacktrackingLevel Senior–Staff~35 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

On a 3x3 Android unlock grid (keys numbered 1-9 row by row), a valid pattern visits between `m` and `n` distinct keys in sequence. Moving between two keys is allowed only if the key in the exact middle of the line between them has already been visited (e.g. going 1->3 requires 2 first; 1->9 requires 5 first). Return the total number of distinct valid unlock patterns of length in [m, n]. 1 <= m <= n <= 9.

Implement
number_of_patterns(m: int, n: int) → int
Examples
in[1,1]out9
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.