Open the Lock
Updated
Problem statement
A lock has four circular wheels. Each wheel shows one digit from 0 through 9. In one move, you may rotate exactly one wheel one step forward or backward; digits wrap between 0 and 9.
The lock starts at "0000". The array deadends lists combinations that immediately block the lock and cannot be entered. Return the minimum number of moves needed to reach target, or -1 if it is impossible.
Function
openLock(deadends: String[], target: String) → intExamples
Example 1
deadends = ["0201","0101","0102","1212","2002"]target = "0202"return = 6One shortest route is 0000, 1000, 1100, 1200, 1201, 1202, 0202. Each step turns one wheel by one, and no step lands on a dead end. The route takes six moves, so the answer is six.
Example 2
deadends = ["8888"]target = "0009"return = 1Digits wrap around, so turning the last wheel back once goes straight from 0000 to 0009. That is a single move, so the answer is one.
Example 3
deadends = ["0000"]target = "8888"return = -1The starting combination is itself a dead end, so the lock is blocked before any move. The target can never be reached, so the answer is minus one.
Constraints
0 <= deadends.length <= 500.- Every combination in
deadendsandtargethas exactly four decimal digits. - All strings in
deadendsare unique.
More Amazon Phone Screen questions
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Spread Through a GridPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Consistent-Hash Packet RouterPHONE SCREEN · Seen Nov 2026
- Count Connected ComponentsPHONE SCREEN · Seen Nov 2026
- Second Largest Distinct ElementPHONE SCREEN · Seen Nov 2026
- Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent ElementsOA · PHONE SCREEN · Seen Nov 2026
- Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Linked Merchants Within Two HopsStripe · PHONE SCREENStripe interview questions
- Shortest Word Chain Between Two WordsFanatics · PHONE SCREEN
- Shortest Currency Conversion with Three-Decimal RateMaven Clinic · PHONE SCREENMaven Clinic interview questions
- Find Duplicate Files in a FilesystemHarvey · PHONE SCREEN · ONSITE INTERVIEWHarvey interview questions
- Shortest Word Transformation SequenceReddit · PHONE SCREENReddit interview questions
- Word LadderGoldman Sachs · PHONE SCREEN · ONSITE INTERVIEWGoldman Sachs interview questions
- Word Ladder IIBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Shortest Path from Encoded Graph EdgesBitGo · OABitGo interview questions
- Word LadderGoogle · ONSITE INTERVIEWGoogle interview questions
- Minimum Word Ladder LengthSalesforce · ONSITE INTERVIEWSalesforce interview questions
Amazon interview experiences
- Amazon Experienced SDE Onsite Questions: 1 Experience
- Amazon SDE Onsite Interview Questions: 9 Experiences
- Amazon SDE II and L5 FDE Interview Questions: 4 Experiences
- Amazon Full-Time OA Problems, Earlier Reports (Part 5 of 5)
- Amazon Full-Time OA Problems, Recent Reports (Part 4 of 5)
- Amazon New Grad OA Problems: Every Task We Have (Part 3 of 5)