Quick Overview

This question evaluates a candidate's ability to perform vectorized data manipulation and numerical validation with Python/NumPy, covering random matrix generation, column normalization, handling edge cases like all-zero columns, and producing unit tests plus time and space complexity analysis.

Generate binomial matrix and column-normalize

Company: Google

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Technical Screen

Using Python with NumPy, generate a 100×100 matrix of Binomial(n = 10, p = 0.3) draws with a fixed random seed, then normalize each column so it sums to 1. Ensure vectorized code, guard against any all-zero column by leaving it as all zeros or replacing with a uniform distribution (state your choice), and verify numerically that column sums are 1 within floating-point tolerance. Provide time and space complexity and a brief unit test.

Overview: This question evaluates a candidate's ability to perform vectorized data manipulation and numerical validation with Python/NumPy, covering random matrix generation, column normalization, handling edge cases like all-zero columns, and producing unit tests plus time and space complexity analysis.

Read the full Google Data Scientist interview experience this question came from

You are given a table that represents entries of a numeric matrix. Each row in the table corresponds to a single cell identified by (row_id, col_id), and the value column stores an integer draw from a Binomial(10, 0.3) distribution. Write a single SQL query that returns each cell along with a normalized_value such that: 1. For each col_id, the sum of normalized_value over all rows with that col_id is 1. 2. If a column's total sum of value is 0, then all normalized_value entries in that column must remain 0 (i.e., do not attempt to normalize an all-zero column). Your query should output the columns: row_id, col_id, normalized_value. Use a set-based, vectorized approach (no row-by-row procedural logic).

Tables

matrix_values(row_id INT, col_id INT, value INT)

Hints

  1. First compute the sum of value per col_id using a window function.
  2. Use a CASE expression to divide by the column sum only when it is greater than zero, and ensure you cast to a decimal type to avoid integer division.

Loading coding console...