Solve palindrome pairs and key path
Company: Airbnb
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Overview: This pair of problems evaluates proficiency with string-processing algorithms and advanced data structures for matching, alongside state-space graph search techniques and state-encoding strategies.
Constraints
- 1 <= m, n <= 30
- Grid size m * n <= 900
- Grid characters are '@', '#', '.', 'a'-'f', 'A'-'F' only
- Exactly one start cell '@'
- At most 6 distinct keys ('a'..'f')
- Movement allowed in 4 directions (up, down, left, right) with cost 1 per step
- You cannot move outside the grid or into walls '#'
Hints
- Use BFS where each state is (row, col, keyMask).
- Represent collected keys with a bitmask; bit i corresponds to key chr(ord('a')+i).
- Precompute the target key mask by scanning the grid.
- Maintain visited states by (row, col, keyMask) to avoid revisiting.
- When encountering a lock 'A'-'F', ensure the corresponding bit is set in keyMask before moving through.