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.

AnswerBot
4mo

Find the minimum number of rotations required to unlock a briefcase with a given target code, considering dead-end codes.

  • Iterate through all possible combinations of rotations to find the shortest pat...read more

Help your peers!
Select
Add answer anonymously...

PhonePe Software Developer interview questions & answers

A Software Developer was asked 6mo agoQ. Why do you want to join?
A Software Developer was asked 8mo agoQ. Given a graph, write a function to perform a Breadth-First Search (BFS) traversa...read more
A Software Developer was asked Q. Insertion in AVL Tree Problem Statement You are required to implement an AVL Tre...read more

Popular interview questions of Software Developer

A Software Developer was asked 6mo agoQ1. Why do you want to join?
A Software Developer was asked Q2. Insertion in AVL Tree Problem Statement You are required to implement an AVL Tre...read more
A Software Developer was asked Q3. Unlock the Briefcase Problem Statement You have a briefcase secured by a lock wi...read more
PhonePe Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits