Fewest Dial Turns to Open a Four-Digit Locker Combination

Last updated: November 29, 2026

Quick Overview

An Amazon Locker service panel has four wheels, each showing 0 to 9 and wrapping around. One turn moves a single wheel by one step. Starting from 0000 and avoiding a list of lockout codes, return the minimum turns to reach the target code, or -1.

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

0

0

0 solved


An Amazon Locker service panel has four wheels, each showing 0 to 9 and wrapping around. One turn moves a single wheel by one step. Starting from 0000 and avoiding a list of lockout codes, return the minimum turns to reach the target code, or -1.

An Amazon onsite BFS problem themed on Amazon Locker maintenance panels. The interviewer checks that you see the implicit graph of 10,000 states, run BFS for the shortest path, and treat lockout codes as removed nodes including the start.

What the Interviewer Expects
  • Model each 4-digit code as a node with 8 neighbors
  • Use BFS for the shortest number of turns
  • Treat lockout codes as visited before starting
  • Return -1 if the start itself is locked out
  • Mention bidirectional BFS as an optimization
Key Topics to Cover
BFS
Implicit Graphs
Shortest Path
State Space Search
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
  • Implement bidirectional BFS and estimate the speedup.
  • What if turning one wheel costs differently per wheel?
  • How would you generalize to n wheels with base b digits?
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

Input: list lockouts of 4-character digit strings and a target string. Output: minimum number of single-wheel moves from 0000 to target without ever landing on a lockout code, or -1....

High-Level Design

States are the 10,000 four-digit strings; each has eight neighbors (each wheel up or down with wraparound). All moves cost 1, so BFS from 0000 finds the shortest path. Seed the visited set with the ...


Submit Your Answer
Markdown supported