Interview Questions/Coding/Firewall Dial Vault

Firewall Dial Vault

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Firewall Dial Vault

A firewall vault has four rotating digit dials and starts at code 0000. In one move, you may rotate exactly one dial one step forward or one step backward. Digits wrap around, so rotating 9 forward gives 0 and rotating 0 backward gives 9. Some four-digit codes are blocked by the firewall. The vault cannot move into a blocked code at any point. If the starting code is blocked, no attempt can begin. Find the minimum number of dial rotations needed to reach the target code. If the target cannot be reached safely, report -1. Input Format: You are given an array blockedCodes and a string targetCode. Output Format: Return the minimum number of moves needed to reach targetCode from 0000, or -1 if no safe route exists.

Examples

Example 1
Input:
blockedCodes = ["0201", "0101", "0102", "1212", "2002"]
targetCode = "0202"
Output:
6

Explanation: One shortest route is 0000 -> 9000 -> 9100 -> 9200 -> 9201 -> 9202 -> 0202. It uses 6 rotation(s), so the output is 6.

Approach hint

Every move has equal cost, so BFS is the natural fit.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

solution.cpp