Cracking the Safe - Problem

You're tasked with cracking a digital safe that has a unique password verification system. The safe requires a password consisting of n consecutive digits, where each digit can range from 0 to k-1.

Here's the catch: the safe doesn't reset between attempts. Instead, it continuously monitors the last n digits you've entered. Every time you input a new digit, the safe checks if the most recent n digits match the correct password.

Example: If the password is "345" (n=3) and you enter "012345":

  • After typing 0: recent digits = "0"
  • After typing 1: recent digits = "01"
  • After typing 2: recent digits = "012"
  • After typing 3: recent digits = "123"
  • After typing 4: recent digits = "234"
  • After typing 5: recent digits = "345"Safe unlocked!

Goal: Find the shortest possible string that is guaranteed to unlock the safe, regardless of what the actual password is. This string must contain every possible n-digit combination as a substring.

Input & Output

example_1.py — Basic case
$ Input: n = 1, k = 2
Output: "10"
💡 Note: For n=1, we need every single digit from 0 to k-1. The string "10" contains both "0" and "1" as substrings.
example_2.py — Small De Bruijn sequence
$ Input: n = 2, k = 2
Output: "00110"
💡 Note: We need all 2-digit binary combinations: "00", "01", "10", "11". The string "00110" contains: "00" (positions 0-1), "01" (positions 1-2), "11" (positions 2-3), "10" (positions 3-4).
example_3.py — Edge case
$ Input: n = 1, k = 1
Output: "0"
💡 Note: With only one possible digit (0) and length 1, the answer is simply "0".

Visualization

Tap to expand
🗝️ Master Key Sequence ConstructionSafe 1Password: 00Safe 2Password: 01Safe 3Password: 10Safe 4Password: 11Master Key Sequence0 0 1 1 0Contains 00Contains 01Contains 11Contains 10De Bruijn Graph Construction:Node: 0Node: 10101Eulerian path visits each edge exactly onceResult: Shortest possible sequence!
Understanding the Visualization
1
Model as Graph
Create a graph where each (n-1)-digit sequence is a node
2
Add Transitions
Connect nodes with edges representing digit additions
3
Find Eulerian Path
Use DFS to visit every edge (combination) exactly once
4
Build Sequence
The path gives us the optimal De Bruijn sequence
Key Takeaway
🎯 Key Insight: By modeling combinations as graph edges rather than nodes, we can find the shortest path that visits every combination exactly once, creating the optimal De Bruijn sequence.

Time & Space Complexity

Time Complexity
⏱️
O(k^n)

Visit each of the k^n edges exactly once in DFS

n
2n
Linear Growth
Space Complexity
O(k^n)

Store visited edges and recursion stack

n
2n
Linearithmic Space

Constraints

  • 1 ≤ n ≤ 4
  • 1 ≤ k ≤ 10
  • 1 ≤ kn ≤ 4096
  • The answer is guaranteed to exist and be unique
Asked in
Google 15 Amazon 8 Meta 6 Microsoft 4
18.5K Views
Medium Frequency
~35 min Avg. Time
567 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen