Unlock the Briefcase Problem Statement
You have a briefcase secured by a lock with 4 circular wheels. The password is a sequence of 4 digits. Each wheel has 10 slots labeled ‘0’ through ‘9’. The wheels can rotate freely and wrap around, with ‘9’ wrapping back to ‘0’. Initially, the lock is set to “0000”.
Given is a list of dead-end codes, each 4 digits long. If at any point the lock shows a dead-end code, the lock stops turning and cannot be opened. Your task is to determine the minimum number of total turns required to unlock the briefcase to a target code or return -1 if it is not feasible.
Your function should take multiple test cases into account, with each specifying dead-end codes and a target code.
Input:
The first line contains an integer ‘T’, the number of test cases.
Each test case consists of:
- An integer ‘N’, the number of dead-end codes.
- ‘N’ single-space separated strings that represent dead-end codes.
- A string ‘target’ representing the unlocking code.
Output:
For each test case, output a single line with an integer representing the minimum number of rotations required. If impossible, output -1.
Example:
Suppose T = 2
:
Input:
2
3
0201 0101 1011
0202
2
8888
0009
8888
Output:
6
-1
Explanation:
In the first test case, the minimum number of turns to unlock the code to '0202' is 6. In the second test case, it's impossible to reach '8888' because it's a dead-end code.
Constraints:
1 <= T <= 10
1 <= N <= 5 * 10^4
|TARGET| = 4
The time limit is set to 1 second.
Top PhonePe Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in PhonePe Software Developer
Reviews
Interviews
Salaries
Users/Month