PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates debugging and implementation-level skills in tensor and array manipulation, focusing on aliasing behavior, conversion between column lists and ndarrays, correct handling of non-divisible chunking, and writing tests for these issues.

  • medium
  • Waymo
  • Coding & Algorithms
  • Machine Learning Engineer

Debug Tensor Conversion Bugs

Company: Waymo

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given a small tensor utility library used in distributed numerical code. The implementation has several bugs related to aliasing, array conversion, and dropping remainder elements. Identify the bugs, fix the implementation, and add tests. ```python import numpy as np class Matrix: def __init__(self, data): self.data = data @staticmethod def zeros(rows, cols): # Intended: create a rows by cols matrix where each cell is independent. return Matrix([[0] * cols] * rows) def to_ndarray(columns): # columns is a list of one-dimensional arrays of equal length. # Intended output shape: (n, k), where k is the number of columns. return np.stack(columns) def from_ndarray(arr, chunk_size): # Intended: split arr along axis 0 into chunks of size chunk_size. # The final chunk should contain the remaining rows if arr.shape[0] # is not divisible by chunk_size. chunks = [] for i in range(arr.shape[0] // chunk_size): chunks.append(arr[i * chunk_size:(i + 1) * chunk_size]) return chunks ``` Requirements: 1. Mutating one row of `Matrix.zeros(rows, cols)` must not mutate other rows. 2. `to_ndarray([a, b, c])` should return an array whose columns are `a`, `b`, and `c`. 3. `from_ndarray` must not silently drop remainder rows. 4. Add tests for aliasing, shape correctness, and non-divisible chunk sizes.

Quick Answer: This question evaluates debugging and implementation-level skills in tensor and array manipulation, focusing on aliasing behavior, conversion between column lists and ndarrays, correct handling of non-divisible chunking, and writing tests for these issues.

A small tensor utility library has three independent bugs: shared-row aliasing in zero-matrix creation, incorrect column-to-2D conversion, and chunking that drops leftover rows. Implement the corrected behavior. Write a function `solution(rows, cols, updates, columns, arr, chunk_size)` that performs all three tasks: 1. Create a `rows x cols` zero matrix with independent rows, then apply each update `[r, c, value]` to that matrix. 2. Convert `columns`, a list of equal-length 1D lists, into a 2D array whose columns are exactly those lists. If `columns = [a, b, c]`, the result should have rows `[a[i], b[i], c[i]]`. 3. Split `arr` along axis 0 into chunks of size `chunk_size`. If the number of rows is not divisible by `chunk_size`, the final chunk must contain the remaining rows. Return a single list of 2D arrays in this order: - index 0: the updated zero matrix - index 1: the converted 2D array from `columns` - index 2 onward: each chunk from `arr` You may solve this using standard Python lists; NumPy is not required.

Constraints

  • 0 <= rows, cols <= 200
  • 0 <= len(updates) <= rows * cols
  • All update indices are valid for the created matrix
  • All lists in `columns` have the same length
  • 1 <= chunk_size
  • `arr` is a rectangular 2D list

Examples

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

Expected Output: [[[0, 7, 0], [0, 0, 0]], [[1, 3, 5], [2, 4, 6]], [[1, 2], [3, 4]], [[5, 6]]]

Explanation: Updating row 0 must not affect row 1. The three input lists become columns of a 2x3 array, and the 3-row array is split into chunks of sizes 2 and 1.

Input: (3, 2, [(1, 0, 5)], [[9], [8]], [[10, 11], [12, 13], [14, 15], [16, 17]], 3)

Expected Output: [[[0, 0], [5, 0], [0, 0]], [[9, 8]], [[10, 11], [12, 13], [14, 15]], [[16, 17]]]

Explanation: Only the middle row should change, which catches the shared-row aliasing bug. The converted array has shape 1x2, and the last row remains in its own chunk.

Input: (1, 1, [(0, 0, 1)], [], [], 4)

Expected Output: [[[1]], []]

Explanation: This edge case has no columns and an empty array to chunk. The converted 2D array is empty, and no chunks are produced.

Input: (2, 2, [(0, 0, -1), (1, 1, 3)], [[-1, 0, 1], [2, 2, 2]], [[7], [8]], 1)

Expected Output: [[[-1, 0], [0, 3]], [[-1, 2], [0, 2], [1, 2]], [[7]], [[8]]]

Explanation: Negative values are allowed in updates. Chunk size 1 means every row of `arr` becomes its own chunk.

Hints

  1. Using `[[0] * cols] * rows` creates multiple references to the same inner list. Build each row separately.
  2. If the inputs are columns, build the converted array row by row using the same index from each column. For chunking, iterate from 0 to `len(arr)` in steps of `chunk_size`.
Last updated: May 23, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Validate a Parent-Child Forest - Waymo (medium)
  • Expand Nested Repetition Expressions - Waymo (medium)
  • Find Largest Adjacent Sorted Difference - Waymo (medium)
  • Find Shortest Paths to Target Nodes - Waymo (medium)
  • Implement Safe Average Function - Waymo (medium)