Determine Safe Bomb Defusal
Company: Mithril
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates competency in graph traversal, state-space search, reachability analysis, and handling safety constraints within finite state machines. It is commonly asked in Coding & Algorithms interviews to test practical application of algorithmic reasoning—especially constrained pathfinding, cycle handling, and edge-case analysis—rather than purely theoretical understanding.
Constraints
- 0 <= len(transitions) <= 200000
- The number of distinct states is finite and can be up to 100000
- State names and action names are case-sensitive strings
- goalStates and unsafeStates may be empty
Examples
Input: ('armed', {'done'}, {'boom'}, [('armed', 'cut_red', 'w1'), ('w1', 'press_button', 'done'), ('armed', 'cut_blue', 'boom')])
Expected Output: (True, ['cut_red', 'press_button'])
Explanation: The safe path is armed -> w1 -> done using actions cut_red, press_button. The cut_blue transition is illegal because it enters an unsafe state.
Input: ('safe', {'safe', 'done'}, {'boom'}, [('safe', 'wait', 'boom')])
Expected Output: (True, [])
Explanation: The initial state is already a goal state and is not unsafe, so no actions are needed.
Input: ('done', {'done'}, {'done'}, [('done', 'wait', 'other')])
Expected Output: (False, [])
Explanation: Unsafe states fail immediately. Since the initial state is unsafe, there is no legal safe sequence.
Input: ('s', {'g'}, {'x'}, [('s', 'to_a', 'a'), ('a', 'loop', 's'), ('a', 'trap', 'x'), ('a', 'to_b', 'b'), ('b', 'finish', 'g')])
Expected Output: (True, ['to_a', 'to_b', 'finish'])
Explanation: There is a cycle between s and a, but the safe route is s -> a -> b -> g. The transition to x is illegal because x is unsafe.
Input: ('start', {'done'}, {'fail'}, [('start', 'left', 'mid'), ('start', 'right', 'mid'), ('mid', 'finish', 'done')])
Expected Output: (True, ['left', 'finish'])
Explanation: Both left and right reach the same state in one step. Since transitions are explored in input order, the shortest returned path uses left first.
Input: ('start', {'done'}, {'fail'}, [('start', 'step', 'mid')])
Expected Output: (False, [])
Explanation: The state mid is a dead end and is not a goal state, so no safe defusal path exists.
Input: ('start', set(), {'fail'}, [('start', 'go', 'mid'), ('mid', 'finish', 'done')])
Expected Output: (False, [])
Explanation: There are no goal states at all, so reaching a safe defused state is impossible.
Hints
- Model the states as graph nodes and the transitions as directed edges.
- Because cycles are allowed and you need the shortest safe action sequence, think about BFS plus storing parent information to rebuild the path.