FastPrepOpen the Lock and Return a Shortest Path
Problems/Open the Lock and Return a Shortest Path

Open the Lock and Return a Shortest Path

Zip logoZip● MediumFULLTIMEPHONE SCREENONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The reports identify Open the Lock and ask for a shortest route plus the visited lock states. They omit a tie rule and some edge behavior; the judged core task matches the available source at about 94%.

A lock has four circular wheels, and each wheel contains the digits 0 through 9. In one move, you may turn exactly one wheel forward or backward by one digit. Turning forward from 9 produces 0, and turning backward from 0 produces 9.

Given a starting combination start, a desired combination target, and an array blocked of combinations that the lock may never enter, return a shortest valid sequence of combinations from start through target, inclusive.

If several shortest paths exist, return the lexicographically smallest sequence. If no valid path exists, return an empty array.

Function

shortestLockPath(start: String, target: String, blocked: String[]) → String[]

Examples

Example 1

start = "0000"target = "0202"blocked = ["0201","0101","0102","1212","2002"]return = ["0000","0001","0002","0003","0103","0203","0202"]

The sequence uses six moves, which is minimum. Among all six-move paths, this sequence is lexicographically smallest.

Example 2

start = "0000"target = "0009"blocked = []return = ["0000","0009"]

Turning the last wheel backward wraps directly from 0 to 9.

Example 3

start = "0000"target = "0000"blocked = ["0000"]return = []

The starting combination is blocked, so no valid path exists.

Constraints

  • start.length == target.length == 4.
  • Every combination contains only digits from 0 through 9.
  • 0 <= blocked.length <= 10000.
  • All strings in blocked are distinct.

More Zip problems

  • Maximum Survival Time in a Graph PursuitPHONE SCREEN · Seen Sep 2026
  • Find All Zip Rummy MeldsPHONE SCREEN · ONSITE INTERVIEW · Seen Apr 2026
  • Shortest Path Around Moving ParadesPHONE SCREEN · Seen Mar 2026
All Zip interview questionsSee Zip hiring insights
Saved
class Solution {
    public String[] shortestLockPath(String start, String target, String[] blocked) {
        // Write your code here.
    }
}
Checking account…