Dynamically allocate a k-dimensional array
Filed under Adobe
Free read 1 of 3Understand the target language (C/C++ vs Python/Java) and how multidimensional bracket indexing is expected to behave.
The question
Write a function that takes dimension sizes as parameters and dynamically allocates memory for an arbitrary k-dimensional array so that elements can be accessed using standard multi-dimensional bracket notation.
Constraints
- Dimensions list contains positive integers.
- Memory allocation must be contiguous or properly managed via pointer arrays to avoid leaks.
- Total size should not exceed memory limits.
Examples
- Input
- dims = [2, 3]
- Output
- A 2x3 allocated array accessible as arr[i][j]
- Why
- Allocates a 2D array with 2 rows and 3 columns.
Approach
Understand the target language (C/C++ vs Python/Java) and how multidimensional bracket indexing is expected to behave.
Create nested arrays of pointers manually using multiple loops depending on the depth.
Flatten the allocation into a single contiguous block of memory and set up pointer arrays or use modular arithmetic mapping for arbitrary depth k.
Handle empty dimension lists, zero-sized dimensions, and extreme depth leading to stack/heap limits.
Tips
- What they are testing: Test with 1D, 3D, and degenerate dimensions like 1 to ensure pointer indexing holds.
- Common mistake: Failing to clean up memory correctly, resulting in memory leaks across nested allocations.
Related Adobe questions
Collected from public candidate posts and coding forums. Updated Nov 2026.