FastPrepAllocate and Release Dynamic Matrices
Problems/Allocate and Release Dynamic Matrices

Allocate and Release Dynamic Matrices

AMD logoAMD● MediumFULLTIMEONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The experienced SDE2 account asks for functions that allocate and deallocate memory for a dynamically sized matrix. This finite multi-matrix lifecycle, tuple interface, zero initialization, error results, id reuse and bounds are authored to make that task executable. It remains separate from the matrix-multiplication question. C++ references perform actual array cleanup; other references explicitly drop storage references. Approximate source match 75%.

Implement a matrix owner that allocates storage with dimensions supplied at runtime, supports indexed reads and writes, and releases that storage. Process the following operations in order. Return one integer per operation.

  • [0,id,rows,cols,value]: create a zero-filled matrix under an unused id. Return 0, or -1 if that id is active.
  • [1,id,row,col,value]: write a cell. Return 0, or -1 if the matrix or index is invalid.
  • [2,id,row,col,value]: return the cell, or -2147483648 if the matrix or index is invalid.
  • [3,id,a,b,value]: release an active matrix and return 0, or return -1 for an inactive id.

A released id may be created again and must start with zeros. Matrices own independent storage. Ignore unused tuple entries. Release any still active matrices before returning. Implement both a separately allocated row layout and a flat row-major layout.

In C++, allocate and release the actual arrays. In garbage-collected languages, release means clearing the owner's storage references; this does not promise immediate physical reclamation. The numeric judge checks the observable lifecycle, indexing and isolation; code review checks storage ownership and cleanup.

Function

runMatrixMemory(operations: int[][]) → int[]

Examples

Example 1

operations = [[0,0,2,2,0],[1,0,1,1,7],[2,0,1,1,0],[3,0,0,0,0],[2,0,1,1,0]]return = [0,0,7,0,-2147483648]

Creation fills four cells with zero. Writing [1][1] stores 7. After release, id 0 is inactive, so its next read returns the missing-cell sentinel.

Example 2

operations = [[0,0,1,1,0],[1,0,0,0,9],[3,0,0,0,0],[0,0,1,1,0],[2,0,0,0,0]]return = [0,0,0,0,0]

Recreating a released id allocates fresh zero-filled storage. Its old value 9 does not carry over.

Constraints

  • 0 <= operations.length <= 200.
  • Every operation has exactly five entries.
  • -1000000 <= operations[i][j] <= 1000000.
  • Each row is [kind,id,a,b,value]. Kind is 0..3 and id is 0..15. For create (kind 0), a and b are dimensions in 1..16. For write (1) and read (2), a and b are candidate indices in 0..16. Release (3) ignores a,b,value. All unused entries may be any number in the global value range.

More AMD problems

  • Insert After a Linked-List NodeONSITE INTERVIEW · Seen Oct 2026
  • LRU CacheONSITE INTERVIEW · Seen Oct 2026
  • Lowest Common Ancestor of a Binary TreePHONE SCREEN · Seen Oct 2026
All AMD interview questionsSee AMD hiring insights
Saved
public int[] runMatrixMemory(int[][] operations) {
    // Write your code here.
}
Checking account…