Solve array-sum and city-community problems
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates array-processing and grid/graph connectivity skills, covering counting subarrays with a target sum and identifying connected building communities in a grid; it belongs to the Coding & Algorithms domain and involves streaming/online algorithm reasoning as well as grid traversal concepts.
Part 1: Count contiguous segments with a target sum
Constraints
- 0 <= len(nums) <= 200000
- -10^9 <= nums[i] <= 10^9
- -10^9 <= k <= 10^9
- The answer fits in a signed 64-bit integer.
Examples
Input: ([1, 1, 1], 2)
Expected Output: 2
Explanation: The valid subarrays are [1, 1] at indices 0..1 and 1..2.
Input: ([1, 2, 3], 3)
Expected Output: 2
Explanation: The valid subarrays are [1, 2] and [3].
Input: ([3, 4, 7, 2, -3, 1, 4, 2], 7)
Expected Output: 4
Explanation: There are four subarrays with sum 7.
Input: ([], 0)
Expected Output: 0
Explanation: An empty array has no contiguous subarrays.
Input: ([0, 0, 0], 0)
Expected Output: 6
Explanation: All possible subarrays sum to 0, and there are 6 of them.
Hints
- Use prefix sums. If the current prefix sum is p, then any earlier prefix sum of p - k forms a valid subarray ending here.
- Store how many times each prefix sum has appeared so far in a hash map.
Part 2: Online target-sum subarray counting for a stream
Constraints
- 0 <= len(stream) <= 200000
- -10^9 <= stream[i] <= 10^9
- -10^9 <= k <= 10^9
- The running totals fit in a signed 64-bit integer.
Examples
Input: ([1, 1, 1], 2)
Expected Output: [0, 1, 2]
Explanation: After each step, the total number of valid subarrays becomes 0, then 1, then 2.
Input: ([3, 4, 7, 2, -3, 1, 4, 2], 7)
Expected Output: [0, 1, 2, 2, 2, 3, 3, 4]
Explanation: This shows the running total after each arrival.
Input: ([], 5)
Expected Output: []
Explanation: No arrivals means no running totals.
Input: ([0, 0, 0], 0)
Expected Output: [1, 3, 6]
Explanation: Each new zero creates several additional zero-sum subarrays.
Input: ([5], 5)
Expected Output: [1]
Explanation: The single-element subarray [5] matches the target.
Hints
- You do not need the full history of elements. Maintain only the current prefix sum, the running answer, and counts of previous prefix sums.
- When a new prefix sum p arrives, add the number of earlier times you have seen p - k.
Part 3: Count independent building communities in a city grid
Constraints
- 0 <= number of rows <= 300
- 0 <= number of columns <= 300
- grid[r][c] is either 0 or 1
Examples
Input: [[1, 1, 0, 0], [0, 1, 0, 1], [0, 0, 1, 1], [1, 0, 0, 0]]
Expected Output: 3
Explanation: There are three separate groups of buildings.
Input: []
Expected Output: 0
Explanation: An empty grid contains no communities.
Input: [[1]]
Expected Output: 1
Explanation: A single building cell forms one community.
Input: [[1, 0], [0, 1]]
Expected Output: 2
Explanation: Diagonal cells do not connect, so these are two separate communities.
Input: [[1, 1, 1], [1, 1, 1]]
Expected Output: 1
Explanation: All building cells are connected.
Hints
- Whenever you find an unvisited building cell, you have discovered a new community.
- Use DFS or BFS to mark every building cell in that community before continuing.
Part 4: Count communities in a huge grid streamed row by row
Constraints
- 0 <= len(rows)
- All rows have the same length
- Each row contains only '0' and '1'
- The algorithm should use O(columns) additional memory
Examples
Input: []
Expected Output: 0
Explanation: No rows means no communities.
Input: ['']
Expected Output: 0
Explanation: A grid with zero columns contains no buildings.
Input: ['1100', '1100', '0011', '0011']
Expected Output: 2
Explanation: The top-left block and the bottom-right block are separate communities.
Input: ['101', '111']
Expected Output: 1
Explanation: The two top cells become connected through the second row.
Input: ['1001', '0000', '1001']
Expected Output: 4
Explanation: Each isolated building group is separated by empty rows or columns.
Hints
- When scanning left to right, a building cell only needs to consider its left neighbor in the current row and its upper neighbor from the previous row.
- If a component appears in the previous row but not in the current row, it can never connect to future rows again and can be finalized.