KatInterview

Sparse Vector Overlap Sum and Land Cluster Counter

Trees & GraphsAuto-graded · 17 test cases
MediumAuto-gradedTrees & Graphstwo-pointerssparse-vectorgrid

Last updated

Combine a merge-style sparse vector dot product with an iterative flood fill that counts 4-connected land clusters, returning both results together.

Write one function that answers two independent questions and returns both answers together.

Part A: Sparse vector dot product

Two long vectors of the same length n are stored sparsely. Each vector is a list of [index, value] pairs, one pair for each position holding a non-zero value. Every position not listed is zero. Each list is sorted by index in increasing order, and an index appears at most once per list.

Compute the dot product of the two vectors, that is, the sum over all positions of the product of the two entries at that position. Only positions present in both lists can contribute. Values may be negative.

Part B: Counting land clusters

You are also given a rectangular grid whose cells hold "1" (land) or "0" (water). A cluster is a maximal group of land cells in which any two cells are linked by a chain of land cells, each step moving up, down, left, or right. Cells that touch only diagonally are not connected.

Count the clusters in the grid. An empty grid, or a grid with no land, has zero clusters. A land cell may also be given as the integer 1; treat it the same as "1".

Output

Return a list of two integers: [dot_product, cluster_count].

Constraints

  • n is the common length of both vectors and can be as large as 100,000. Every index is in [0, n-1].
  • Each sparse list is sorted by index, with no repeated indices within a list. Either list may be empty.
  • Values are integers and may be negative.
  • The grid may be empty, may be a single row or column, and may be large, so a deep recursion can overflow the stack. Prefer an explicit stack or queue.
  • The running time should be proportional to the number of stored entries for Part A, and to the number of grid cells for Part B.
  • You may modify the grid in place or use extra memory.

Examples

Example 1

Input: sparse_overlap_and_land_clusters([[0, 2], [3, 4]], [[0, 5], [2, 7]], 5, [['1', '0'], ['0', '1']])

Output: [10, 2]

Example 2

Input: sparse_overlap_and_land_clusters([[1, -2], [4, 3]], [[0, 8], [1, 5], [4, -1]], 6, [['1', '1', '0'], ['0', '1', '0'], ['0', '0', '1']])

Output: [-13, 2]

Example 3

Input: sparse_overlap_and_land_clusters([], [], 0, [])

Output: [0, 0]

Hints

Hint 1

Both lists are sorted by index, so you can walk them together with two pointers, like the merge step of merge sort. You never need to build dense arrays of length n.

Hint 2

When the indices at the two pointers differ, the entry with the smaller index has no partner, so skip it. When they match, multiply and advance both pointers.

Hint 3

For the grid, scan every cell. Each time you meet unvisited land, add one to the count and flood-fill its whole cluster with an explicit stack or queue, marking cells as visited when you push them.