← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Apr 2026

Summary

Meta coding round, and they took the classic grid BFS problem and added a whole layer of keys and locked doors on top of it. Not the hardest thing in the world if you've seen it before, but I hadn't really drilled the bitmask state extension and it showed.

Questions Asked (1)

Q1

Given a grid where some cells are locked doors and others contain keys, find the shortest path from start to goal. Collecting a key permanently unlocks all doors of that color. How do you solve this efficiently?

Algorithms & Data Structures
Author's notes

I started with plain BFS and the interviewer immediately asked what happens when the same cell is visited twice but with different keys collected.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the problem as a shortest path in a state space where each state is (position, set of collected keys). Use BFS to explore states level by level, since each move has uniform cost. When a key is collected, update the key set and unlock all doors of that color.

Pro tip: Mention that the number of keys is typically small, so bitmask representation is efficient. Also, discuss how to handle multiple keys of the same color—collecting one unlocks all doors of that color, so you only need to track which colors have been collected.

1. Define State Representation

Represent each state as (row, col, key_mask), where key_mask is a bitmask of collected key colors. This captures both position and which doors are unlocked.

2. Initialize BFS

Start BFS from the initial position with an empty key mask. Use a queue and a visited set to avoid revisiting states.

3. Explore Neighbors

For each state, consider moving up, down, left, right. If the neighbor is a wall, skip. If it's a door, only proceed if the corresponding key bit is set. If it's a key, update the key mask by setting the bit for that color.

4. Track Distance and Goal

Maintain distance from start. When the goal cell is reached, return the distance. If BFS exhausts all states without reaching the goal, return -1.

5. Optimize with Bitmask and Visited Set

Use a 3D visited array or a set of encoded integers to track visited states efficiently. Bitmask allows up to 32 key colors.

Key Points to Mention

  • State space includes position and key set, leading to O(R*C*2^K) states.
  • BFS guarantees shortest path because all moves have equal cost.
  • Bitmask efficiently represents key sets, enabling fast state transitions.
  • Doors of the same color are unlocked once the key is collected.
  • Visited set prevents infinite loops and redundant exploration.
  • Time complexity: O(R*C*2^K), space complexity: O(R*C*2^K).

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.