Shortest Path in Binary Matrix
Problem Statement
Designing a pathfinding service that computes the shortest path from the top-left corner to the bottom-right corner of a binary matrix, where cells can either be blocked (1) or open (0).
The movement is allowed in 8 directions (horizontal, vertical, and diagonal), and the goal is to return the minimum number of steps required to reach the destination.
This service will be part of a larger platform (like a robot navigation system, AI grid simulation, or game engine) that frequently queries path computations in real time.
🎯 Problem Statement
Design a scalable and efficient system that:
⚙️ Functional Requirements
Input:
Output:
Movement:
Example:
1
2Input:
3[[0,1],
4 [1,0]]
5
6Output: 2Edge Cases:
🚫 Non-Functional Requirements
Aspect with Requirement
Latency
< 200 ms for grids up to 1000x1000
Scalability
Should handle multiple concurrent requests (batch jobs or API calls)
Accuracy
Must always return the correct minimum path length
Availability
99.9% uptime for real-time systems
Extensibility
Should support additional constraints (e.g., weighted cells, dynamic grids)
🧩 Key Design Components
1. Pathfinding Service
1
2POST /path/shortest1
2{
3 "grid": [[0,1,0],[0,0,0],[1,0,0]]
4}1
2{
3 "shortest_path": 4
4}2. Pathfinding Engine
3. Task Queue / Job Scheduler
4. Cache Layer (Redis / Memcached)
5. Metrics & Monitoring
🧠 Algorithm Design Notes
BFS is used since:
Optimization Techniques:
🧱 System Architecture Overview
1
2[Client / API Gateway]
3 |
4 v
5[Shortest Path Service] -- calls --> [Pathfinding Engine]
6 | |
7 |-----------------> [Cache Layer] <--|
8 |
9 +--> [Job Queue] --> [Worker Nodes]
10 |
11 +--> [Metrics/Logging System]🧩 Data Flow Example
📈 Scalability Enhancements
🧮 Example Metrics
| Metric | Target
| Avg Latency | < 200 ms for 1000×1000 grid
| Max Grid Size | 5000×5000
| Throughput | 1000 requests/sec
| Cache Hit Rate | > 70%
💬 Interview Prompt (Final Form)
Design a Shortest Path Computation Service in a Binary Matrix.
The service should take a binary matrix as input, where 0s represent open cells and 1s represent blocked cells, and return the minimum number of steps required to reach from the top-left to the bottom-right corner, moving in 8 directions.
The system should be designed for scalability, low latency, and concurrent path computation.