google phone screen | passed | dec 2024 | L4 | Bangalore

Difficulty: MEDIUMCategory: InterviewExperienceFrequency: 1Asked On 2024-12-11Added By Anonymous
Source: Link

Company: Google
Role: L4
Interview Stage: Phone Screen
Location: Bangalore
Year: 2024

Original Post Content:

Problem:
You are given a grid of size r × c and n objects located at specific coordinates. You need to find the number of axis-parallel rectangular subgrids that contain at least k of these objects.

Two rectangles are considered different if their coordinates are different.


def count_rectangles(r: int, c: int, n: int, k: int, positions: List[Tuple[int, int]]) -> int:

eg. 1
r = 2
c = 2
n = 1
k = 1
positi [(1, 2)]

ans: 4

r = 3
c = 2
n = 3
k = 3
positi [(1, 1), (3, 1), (2, 2)]

ans: 1

r = 3
c = 2
n = 3
k = 2
positi [(1, 1), (3, 1), (2, 2)]

ans: 4

was able to solve efficiently


function count_rectangles(r_rows, c_cols, k, positions_by_row): // Renamed parameters for clarity to match problem statement and avoid pseudocode variable conflicts
    Initialize ans to 0
    
    // Iterate through each row as the starting point (top boundary of rectangle)
    for i from 0 to r_rows-1:
        // Initialize arrays to store left, right, count, and 'to' values for columns
        // 'r_arr' is used instead of 'r' to avoid conflict with 'r_rows' parameter
        Initialize l, r_arr, cnt, to as arrays of size (c_cols + 2)
        
        // This loop prepares the initial 'cnt' array which sums up object counts per column
        // for all rows from 'i' to 'r_rows-1'.
        // It assumes 'positions_by_row' is a data structure (e.g., list of lists)
        // where positions_by_row[row_idx] gives a list of column indices of objects in that row.
        // If 'positions' from problem statement is List[Tuple[int, int]],
        // it would need to be pre-processed into 'positions_by_row'.
        for x from i to r_rows-1:
            for y in positions_by_row[x]:
                Increment cnt[y]
        
        // Find the left and right bounds for columns based on object counts in 'cnt'
        last = 0
        for j from 1 to c_cols:
            if cnt[j] > 0:
                l[j] = last
                r_arr[last] = j
                last = j
        r_arr[last] = c_cols + 1
        l[c_cols + 1] = last
        
        // Initialize the result for the current configuration (fixed top row 'i', varying bottom row)
        res = 0
        
        // This loop uses a two-pointer/sliding window approach on columns
        // to find valid column ranges [i_ptr, j_ptr] for the current row range.
        // 's' tracks the count of objects in the current column window.
        for i_ptr = r_arr[0], j_ptr = 0, s = 0; i_ptr <= c_cols; i_ptr = r_arr[i_ptr]:
            while j_ptr <= c_cols and s < k:
                j_ptr = r_arr[j_ptr]
                s += cnt[j_ptr]
            res += (i_ptr - l[i_ptr]) * (c_cols + 1 - j_ptr)
            to[i_ptr] = j_ptr
            s -= cnt[i_ptr]
        
        // Define the remove function for updating counts when a row is removed from the bottom
        function remove(x_col): // 'x_col' is a column index of an object
            i_rem = x_col
            while l[i_rem] > 0 and to[l[i_rem]] >= x_col:
                i_rem = l[i_rem]
            Decrement cnt[x_col]
            for j_rem from l[i_rem] to x_col:
                while j_rem <= c_cols and s < k: // 's' variable needs to be scoped correctly for this to work
                    j_rem = r_arr[j_rem]
                    s += cnt[j_rem]
                res -= (i_rem - l[i_rem]) * (j_rem - to[i_rem])
                to[i_rem] = j_rem
                s -= cnt[i_rem]
        
        // Iterate backwards from the last row (bottom boundary of rectangle) to the current top row 'i'
        for d from r_rows-1 down to i:
            ans += res // Add the count of rectangles for the current [i, d] range
            // Remove objects from row 'd' (which is row 'd' if we consider iterating downwards)
            // The pseudocode implies that 'd' represents the bottom row, and when we decrement 'd',
            // we are effectively removing the objects from what was the 'd+1' row
            // in the previous iteration's rectangle range. If 'd' goes from r_rows-1 down to i,
            // then for row 'd' we add 'res'. Then `for x in positions_by_row[d]: Call remove(x)`
            // seems to indicate we remove elements of row 'd' *before* considering the next 'd-1' range.
            // This is the common approach.
            for x_obj_pos in positions_by_row[d]:
                Call remove(x_obj_pos)
    
    Return ans

Comments