← 返回 waymo 的题目列表Debugging Numerical/Tensor API Bugs: Aliasing, Axis Handling, and Truncation
类型:online_judge
You are given a set of pseudo APIs similar to numpy/tensor utilities. The code contains multiple bugs that cause incorrect outputs or unexpected behavior for some inputs. Your tasks:
Identify root causes (including aliasing/shared storage, incorrect axis handling, and truncation of remaining elements in from_ndarray).
Propose concrete fixes (implementation or correct usage).
Provide minimal test cases (inputs + expected outputs) that reproduce and prevent regressions.
Abstract API
Matrix.zeros(shape): returns a zero matrix of given shape.
to_ndarray(matrix, axis=?): converts a custom Matrix to an ndarray, optionally flattening/reordering by an axis.
from_ndarray(arr, shape): converts an ndarray into a Matrix; may truncate/pad if element counts mismatch.
Typical issues to look for
Matrix.zeros may create shared rows (e.g., [[0]*n]*m), so updating one element affects multiple rows.
to_ndarray may ignore or mishandle axis=1.
from_ndarray may silently truncate remainders during reshape, causing data loss.
State the expected behavior, what constitutes a bug, and how unit tests should catch it.
You do not need to provide a full runnable project, but the fixes must be specific enough to implement.
Example
Input
# Test aliasing in zeros
m = Matrix.zeros((2,3))
m[0,0] = 1
print(m)
Output
[[1,0,0],[0,0,0]] # only the first row changes