Array Declaration and Initialization in DSA Javascript - Time & Space Complexity
When we create and fill an array, it takes some time depending on how many items we add.
We want to know how this time grows as the array gets bigger.
Analyze the time complexity of the following code snippet.
const size = n;
const arr = new Array(size);
for (let i = 0; i < size; i++) {
arr[i] = i * 2;
}
This code creates an array of size n and fills it with values.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
forloop that assigns values to each array element. - How many times: Exactly
ntimes, once for each element.
As the array size grows, the number of assignments grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 assignments |
| 100 | 100 assignments |
| 1000 | 1000 assignments |
Pattern observation: The work grows directly in proportion to the size of the array.
Time Complexity: O(n)
This means the time to create and fill the array grows linearly with the number of elements.
[X] Wrong: "Creating an array and filling it is instant and does not depend on size."
[OK] Correct: Each element must be assigned a value, so the time grows with the number of elements.
Understanding how array creation and filling scales helps you reason about performance in many coding problems.
"What if we used Array.fill() instead of a loop? How would the time complexity change?"
Practice
Solution
Step 1: Understand array declaration syntax
In JavaScript, arrays are declared using square brackets [].Step 2: Check each option
const arr = []; uses square brackets which is correct. Options B and C use parentheses and curly braces which are incorrect for arrays. const arr = array(); uses a function call which is not valid.Final Answer:
const arr = []; -> Option BQuick Check:
Array declaration uses [] = const arr = []; [OK]
- Using parentheses () instead of square brackets
- Using curly braces {} which declare objects
- Trying to call array() like a function
Solution
Step 1: Recall array initialization syntax
Arrays are initialized with square brackets containing comma-separated values.Step 2: Evaluate each option
const arr = [1, 2, 3]; correctly uses square brackets with values. Options B and C use parentheses and curly braces which are invalid. const arr = array[1, 2, 3]; uses incorrect syntax with 'array' keyword.Final Answer:
const arr = [1, 2, 3]; -> Option DQuick Check:
Array with values uses [] and commas = const arr = [1, 2, 3]; [OK]
- Using parentheses instead of square brackets
- Using curly braces which create objects
- Trying to use 'array' keyword which doesn't exist
const arr = new Array(3);
console.log(arr);
Solution
Step 1: Understand new Array(n) behavior
Using new Array(3) creates an array with length 3 but no defined values, called a sparse array.Step 2: Check console output for sparse arrays
Console shows sparse arrays as [empty x 3], not as undefined or zeros.Final Answer:
[empty x 3] -> Option CQuick Check:
new Array(3) creates sparse array = [empty x 3] [OK]
- Assuming new Array(n) fills with zeros
- Thinking undefined values appear explicitly
- Expecting a syntax error
const arr = new Array[1, 2, 3];
Solution
Step 1: Identify correct syntax for new Array()
The Array constructor requires parentheses, not square brackets, to pass elements.Step 2: Fix the syntax
Change new Array[1, 2, 3] to new Array(1, 2, 3) to correctly create the array.Final Answer:
Replace square brackets with parentheses: new Array(1, 2, 3); -> Option AQuick Check:
Array constructor uses () not [] = Replace square brackets with parentheses: new Array(1, 2, 3); [OK]
- Using square brackets with new Array
- Removing 'new' keyword incorrectly
- Using curly braces which are invalid here
Solution
Step 1: Understand 2D array initialization
Using Array.from with a function creates separate inner arrays to avoid shared references.Step 2: Analyze each option
const arr = Array(3).fill(Array(2).fill(0)); fills outer array with the same inner array reference, causing changes in one row to affect all. const arr = Array.from({length: 3}, () => Array(2).fill(0)); correctly creates separate inner arrays. const arr = [[0,0],[0,0],[0,0]]; manually creates the array but is less flexible. All of the above is incorrect because only A is fully correct for dynamic initialization.Final Answer:
const arr = Array.from({length: 3}, () => Array(2).fill(0)); -> Option AQuick Check:
Use Array.from with function for separate inner arrays = const arr = Array.from({length: 3}, () => Array(2).fill(0)); [OK]
- Using fill with array causes shared references
- Manually typing arrays is inflexible
- Assuming all options are correct
Ready to prove it?
Pass the test and this pattern moves unknown
