Infosys · Question 66 · Queues
Open the Lock
Problem
Minimum moves to open a lock with deadends and target using BFS.
Approach
BFS through possible states avoiding deadends.
Time O(10^4)Space O(10^4)
How it works
- 1BFS over 4-wheel states level by level.
- 2Skip deadends and visited states.
Sample input / output
Input: deadends = ["0201","0101","0102","1212","2002"], target = "0202" Output: 6
Constraints
1 <= deadends.length <= 500; 4-digit strings.
Solution — read it, then walk it
Time O(10^4)Space O(10^4)Loops 3Branches 5Returns 2source · C++
Scroll sideways for wide charts — the view follows the active step.
- start / end
- statement
- condition
- loop
- returns
- definition
1/41
StartSTART
Interactive control-flow diagram and step-by-step code walkthrough.
Recommended resourcesAffiliate links · may earn us a commission
Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits