Write code for a 2D array.
1Times asked
Apr 2026Last seen
Apr 2026First seen
💡 Model Answer
A 2D array is a collection of arrays arranged in rows and columns. In JavaScript you can create it with nested arrays:
js
const matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
// Access element at row 1, column 2 (0‑based indexing)
const value = matrix[1][2]; // 6In Python it looks similar:
python
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
value = matrix[1][2] # 6You can iterate over rows and columns with nested loops:
js
for (let i = 0; i < matrix.length; i++) {
for (let j = 0; j < matrix[i].length; j++) {
console.log(matrix[i][j]);
}
}Complexity for accessing an element is O(1). Creating the matrix is O(n*m) where n is the number of rows and m the number of columns.
This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.
🎤 Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers — invisible to screen sharing.
Get Assisting AI — Starts at ₹500