Add up BFS ripple values from several taps on an 8x8 grid

Quick Overview

Given tap positions on an 8x8 grid, compute the grid after each tap sends out a 4-directional ripple whose value grows by one with every step, with all ripples summed. It tests breadth-first search on a grid, keeping visited state per source, and careful accumulation.

Add up BFS ripple values from several taps on an 8x8 grid

Company: Pinterest

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given an 8 x 8 grid initialized to all zeros and a list of tap positions. Each tap triggers a ripple effect that spreads outward across the entire grid using BFS, moving in 4 directions: up, down, left and right. The tapped cell receives a value of `1`. Every other cell receives `d + 1`, where `d` is its BFS distance from the tap (the number of 4-directional steps needed to reach it). The ripple spreads until every cell in the grid has been reached. If there are several taps, each tap's ripple values are added (summed) onto the grid. Return the final state of the 8 x 8 grid after processing all taps. ### Function Signature ```python def ripple_effect(taps: list[list[int]]) -> list[list[int]]: ``` ### Rules - Each tap is `taps[i] = [row, col]`, with row 0 at the top and column 0 at the left. - The grid has no obstacles, so every ripple reaches all 64 cells. - The same position may appear more than once in `taps`. Each occurrence is a separate tap and adds its own ripple. - Return the grid as a list of 8 rows, each a list of 8 integers. ### Constraints - The grid is always 8 x 8. - `1 <= len(taps) <= 10` - `0 <= taps[i][0] < 8` - `0 <= taps[i][1] < 8` ### Examples ```text Input: taps = [[0,0]] Output: [[1,2,3,4,5,6,7,8], [2,3,4,5,6,7,8,9], [3,4,5,6,7,8,9,10], [4,5,6,7,8,9,10,11], [5,6,7,8,9,10,11,12], [6,7,8,9,10,11,12,13], [7,8,9,10,11,12,13,14], [8,9,10,11,12,13,14,15]] ``` One tap in the top-left corner. Cell `(r, c)` is `r + c` steps away, so it gets `r + c + 1`, from 1 at the tap up to 15 in the opposite corner. ```text Input: taps = [[3,3],[4,4]] Output: [[16,14,12,10,10,12,14,16], [14,12,10,8,8,10,12,14], [12,10,8,6,6,8,10,12], [10,8,6,4,4,6,8,10], [10,8,6,4,4,6,8,10], [12,10,8,6,6,8,10,12], [14,12,10,8,8,10,12,14], [16,14,12,10,10,12,14,16]] ``` Cell `(3,3)` gets 1 from its own tap plus 3 from the tap at `(4,4)`, which is 2 steps away, for a total of 4. Corner `(0,0)` is 6 steps from `(3,3)` and 8 steps from `(4,4)`, so it gets 7 + 9 = 16. ```text Input: taps = [[0,0],[0,0]] Output: [[2,4,6,8,10,12,14,16], [4,6,8,10,12,14,16,18], [6,8,10,12,14,16,18,20], [8,10,12,14,16,18,20,22], [10,12,14,16,18,20,22,24], [12,14,16,18,20,22,24,26], [14,16,18,20,22,24,26,28], [16,18,20,22,24,26,28,30]] ``` The same corner is tapped twice. Both ripples count, so every value from the first example doubles.

Overview: Given tap positions on an 8x8 grid, compute the grid after each tap sends out a 4-directional ripple whose value grows by one with every step, with all ripples summed. It tests breadth-first search on a grid, keeping visited state per source, and careful accumulation.

Pinterest logo
Pinterest
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given an 8 x 8 grid initialized to all zeros and a list of tap positions. Each tap triggers a ripple effect that spreads outward across the entire grid using BFS, moving in 4 directions: up, down, left and right.

The tapped cell receives a value of 1. Every other cell receives d + 1, where d is its BFS distance from the tap (the number of 4-directional steps needed to reach it). The ripple spreads until every cell in the grid has been reached.

If there are several taps, each tap's ripple values are added (summed) onto the grid. Return the final state of the 8 x 8 grid after processing all taps.

Function Signature

def ripple_effect(taps: list[list[int]]) -> list[list[int]]:

Rules

  • Each tap is taps[i] = [row, col] , with row 0 at the top and column 0 at the left.
  • The grid has no obstacles, so every ripple reaches all 64 cells.
  • The same position may appear more than once in taps . Each occurrence is a separate tap and adds its own ripple.
  • Return the grid as a list of 8 rows, each a list of 8 integers.

Constraints

  • The grid is always 8 x 8.
  • 1 <= len(taps) <= 10
  • 0 <= taps[i][0] < 8
  • 0 <= taps[i][1] < 8

Examples

Input: taps = [[0,0]]
Output: [[1,2,3,4,5,6,7,8],
         [2,3,4,5,6,7,8,9],
         [3,4,5,6,7,8,9,10],
         [4,5,6,7,8,9,10,11],
         [5,6,7,8,9,10,11,12],
         [6,7,8,9,10,11,12,13],
         [7,8,9,10,11,12,13,14],
         [8,9,10,11,12,13,14,15]]

One tap in the top-left corner. Cell (r, c) is r + c steps away, so it gets r + c + 1, from 1 at the tap up to 15 in the opposite corner.

Input: taps = [[3,3],[4,4]]
Output: [[16,14,12,10,10,12,14,16],
         [14,12,10,8,8,10,12,14],
         [12,10,8,6,6,8,10,12],
         [10,8,6,4,4,6,8,10],
         [10,8,6,4,4,6,8,10],
         [12,10,8,6,6,8,10,12],
         [14,12,10,8,8,10,12,14],
         [16,14,12,10,10,12,14,16]]

Cell (3,3) gets 1 from its own tap plus 3 from the tap at (4,4), which is 2 steps away, for a total of 4. Corner (0,0) is 6 steps from (3,3) and 8 steps from (4,4), so it gets 7 + 9 = 16.

Input: taps = [[0,0],[0,0]]
Output: [[2,4,6,8,10,12,14,16],
         [4,6,8,10,12,14,16,18],
         [6,8,10,12,14,16,18,20],
         [8,10,12,14,16,18,20,22],
         [10,12,14,16,18,20,22,24],
         [12,14,16,18,20,22,24,26],
         [14,16,18,20,22,24,26,28],
         [16,18,20,22,24,26,28,30]]

The same corner is tapped twice. Both ripples count, so every value from the first example doubles.