Walking Distance From Every City Block to the Nearest ATM

Last updated: November 29, 2026

Quick Overview

A city map is a grid of walkable blocks, blocked cells and ATM locations. Return, for each walkable block, the walking distance in steps to the nearest ATM, -1 when no ATM is reachable, and None for blocked cells.

Capital One
Coding & Algorithms
Software Engineer
Capital One
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


A city map is a grid of walkable blocks, blocked cells and ATM locations. Return, for each walkable block, the walking distance in steps to the nearest ATM, -1 when no ATM is reachable, and None for blocked cells.

Capital One promotes a large fee-free ATM network and uses coverage analysis when deciding where to place ATMs and cafes. This grid problem tests whether you run one multi-source BFS from all ATMs at once rather than a BFS per cell or per ATM.

What the Interviewer Expects
  • Seed the BFS queue with every ATM at distance 0
  • Expand in four directions with bounds checks
  • Use the distance matrix itself as the visited marker
  • Distinguish blocked cells from unreachable walkable cells
  • Explain why one multi-source BFS is O(rows * cols)
Key Topics to Cover
Multi-source BFS
Grid traversal
Shortest paths in unweighted graphs
Coverage analysis
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you find the single best new ATM location to minimize the worst distance?
  • How would you also return which ATM is nearest to each block?
  • What if some blocks take longer to cross, such as a park?
  • How would you handle a map of a whole metro area that does not fit in memory?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Grid cells: 'A' (ATM), '#' (blocked), '.' (walkable). Movement is up, down, left and right.
  • Output matrix: 0 at ATMs, the step count at walkable cells, -1 for walkable cells cut off from every ATM...
High-Level Design

Running a BFS from every walkable cell costs O((RC)^2). Running one from each ATM and taking the minimum costs O(A * RC). Instead, push all ATMs into one queue at distance 0. BFS processes cells in or...


Submit Your Answer
Markdown supported