Magicsheet logo

Number of Laser Beams in a Bank

Medium
37.5%
Updated 8/1/2025

Asked by 3 Companies

Number of Laser Beams in a Bank

What is this problem about?

The Number of Laser Beams in a Bank problem gives you a bank security system represented as a grid of strings. Each '1' is a security device. Laser beams form between devices in adjacent occupied rows (rows with at least one device). The number of beams between two adjacent occupied rows = (devices in row A) × (devices in row B). Sum all such products. This coding problem requires counting 1s per row and multiplying consecutive non-empty rows.

Why is this asked in interviews?

Meta, Amazon, and Adobe ask this as a clean array and matrix problem that tests the ability to identify the key observation (skip empty rows, multiply adjacent occupied row counts). The array, matrix, math, and string interview pattern is applied directly.

Algorithmic pattern used

Row count scan with adjacent product. Count the number of '1's in each row. Filter out rows with 0 devices (empty rows). For each consecutive pair of non-empty rows, multiply their device counts and add to the total.

Example explanation

Bank:

"011001"
"000000"
"010100"
"001000"

Row 0: 3 devices. Row 1: 0 (skip). Row 2: 2 devices. Row 3: 1 device. Non-empty rows: [3, 2, 1]. Beams: 32 + 21 = 6+2 = 8.

Common mistakes candidates make

  • Not skipping empty rows (multiplying by 0 or including gaps incorrectly).
  • Counting characters other than '1' in the rows.
  • Not iterating over consecutive pairs of non-empty rows.
  • Off-by-one: starting with no "previous row" — track the last non-empty row's count.

Interview preparation tip

This problem decomposes cleanly into two steps: (1) extract device counts per row, filtering empties; (2) compute adjacent pair products. Practice extracting data from 2D grid rows, then applying array computations on the extracted data. This two-step decomposition — extract then compute — is a general strategy for matrix problems that have row/column structure. Getting comfortable with string.count('1') for counting characters in a row is useful for grid string problems.

Similar Questions