Cosmic Module
J
Qubits of DPK
March 14, 2026
Core Java
Layman Explanation
Instead of creating 30 separate variables for 30 students' marks, you create ONE variable that holds all 30 values in order. That's an array — a named row of boxes, each with an index.
Real World Analogy
A train with numbered seats. Seat 0, Seat 1, Seat 2... You can jump to any seat directly if you know the number. That's O(1) access by index.
Creation of Arrays
java
QUBITS OF DPK
Memory Model
javascript
QUBITS OF DPK
Multidimensional Arrays
java
QUBITS OF DPK
Jagged Array
java
QUBITS OF DPK
3D Array
java
QUBITS OF DPK
Default Values
java
QUBITS OF DPK
Advantages
javascript
QUBITS OF DPK
Drawbacks
javascript
QUBITS OF DPK
️ All Traps
java
QUBITS OF DPK
Interview Answer (SDE-2)
"Arrays store multiple values of same type in contiguous memory, providing O(1) index access. Reference lives in stack, actual data in heap. 2D arrays are arrays of arrays — each row is a separate heap object enabling jagged arrays with variable row lengths. Key advantages: fast access, cache-friendly, memory efficient. Key drawback: fixed size — which is why ArrayList exists for dynamic needs. Insert/delete is O(n) due to shifting. Common traps: length is a property not method, index starts at 0, reference copy shares same heap array, for-each loop cannot modify elements."
Interview Questions & MAANG-Level Answers
Q1. What is the time complexity of accessing an element in an array?
O(1) — constant time. Since arrays store elements in contiguous memory, accessing arr[i] is a simple arithmetic: baseAddress + (i * elementSize). No searching needed, no traversal — direct jump to the memory address. This is the biggest advantage of arrays over LinkedLists (which are O(n) for index access).
Q2. Why is insertion at index 0 O(n) for arrays?
Because arrays are fixed contiguous blocks of memory. To insert at index 0, every existing element must shift one position to the right to make space: index 0→0 becomes 1, 1→2, etc. For n elements, that's n shifts — O(n). Same for deletion. This is why ArrayList (backed by array) is slow for frequent insertions/deletions at arbitrary positions, and why LinkedList is preferred when that's the primary operation.
Q3. What is the difference between a regular 2D array and a jagged array?
A regular 2D array has the same number of columns in every row: int[][] matrix = new int[3][4] — 3 rows, each with exactly 4 columns. A jagged array is an array of arrays where each row can have different lengths: int[][] jagged = new int[3][]; jagged[0] = new int[1]; jagged[1] = new int[3];. In Java, 2D arrays are always jagged arrays under the hood — each row is an independent array object in heap.
Q4. How do you deep copy an array in Java?
For 1D arrays: int[] copy = Arrays.copyOf(original, original.length) or System.arraycopy(). Simple int[] copy = original is a SHALLOW copy — both reference same array. For 2D arrays, you need to copy each row separately:
java
QUBITS OF DPK
For object arrays, deep copy requires copying each object too (not just references).
Q5. Why would you use ArrayList over an array?
ArrayList when: you don't know size upfront (dynamic resizing), you need rich API (add, remove, contains, sort), you need to insert/remove elements frequently, or you need to store in a Collection-based API. Array when: size is fixed and known, you need maximum performance (no boxing/unboxing overhead), you're working with primitives, or you need multidimensional data structures (matrix problems).
Q6. What is ArrayIndexOutOfBoundsException?
Thrown when accessing an array with an invalid index — negative or >= array length. Valid indices are 0 to length-1. int[] arr = new int[5]; arr[5] = 10 throws it (valid indices: 0-4). Common cause in DSA: off-by-one errors in loop conditions (i <= arr.length instead of i < arr.length). In production, always validate indices before access or use bounds-safe methods.