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.

Be the first one to answer
Add answer anonymously...
PhonePe Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter