← Amazon Interview Insights

Amazon·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Amazon SWE coding round with a BFS problem on a 2D matrix. The interviewer dialed back the complexity a bit mid-interview, focusing on the minimum steps portion. Not the hardest version of this problem type but still took some thinking.

Questions Asked (1)

Q1

Given a 2D matrix, find the minimum number of steps to reach from a source cell to a target cell using BFS.

Algorithms & Data Structures
Author's notes

The interviewer simplified it partway through which honestly helped me get unstuck.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem constraints (e.g., movement allowed, obstacles, matrix size) and then explain that BFS is optimal for finding the shortest path in an unweighted grid. Walk through the algorithm: initialize a queue with the source, track visited cells, and explore level by level until the target is reached, returning the distance.

Pro tip: Mention that you can optimize space by marking visited cells in-place (e.g., setting them to a special value) if the matrix is mutable, and discuss how to handle edge cases like unreachable target or source equals target.

1. Clarify the problem

Ask about movement directions (4 or 8), obstacles, matrix dimensions, and whether the target is guaranteed reachable. Confirm that each step moves to an adjacent cell.

2. Choose BFS and justify

Explain that BFS explores level by level, guaranteeing the shortest path in an unweighted graph. Contrast with DFS which may not find the shortest path.

3. Outline BFS algorithm

Initialize a queue with the source cell and a distance counter. Use a visited set or modify the matrix to mark visited cells. Process nodes level by level, incrementing distance after each level.

4. Handle edge cases and complexity

Check if source equals target (return 0), if target is unreachable (return -1), and analyze time and space complexity: O(R*C) time and O(R*C) space in worst case.

5. Discuss optimizations and variations

Mention bidirectional BFS for faster search, or using a distance matrix if the matrix cannot be modified. Also note how to handle obstacles by treating them as blocked cells.

Key Points to Mention

  • BFS guarantees shortest path in unweighted graphs
  • Use a queue for level-order traversal
  • Track visited cells to avoid cycles
  • Time complexity O(R*C) where R and C are matrix dimensions
  • Space complexity O(R*C) for the queue and visited set
  • Edge cases: source equals target, unreachable target, obstacles

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