Array Declaration and Initialization in DSA Typescript - Time & Space Complexity
When we create and fill an array, we want to know how long it takes as the array grows.
We ask: How does the time to set up the array change with its size?
Analyze the time complexity of the following code snippet.
const size = n;
const arr: number[] = 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 with the size of the array.
Time Complexity: O(n)
This means the time to fill the array grows in a straight line with its size.
[X] Wrong: "Creating an array is instant and does not depend on size."
[OK] Correct: Even if the array space is reserved quickly, filling each element takes time proportional to the number of elements.
Understanding how array setup time grows helps you explain efficiency clearly and shows you know what happens behind the scenes.
"What if we used a built-in method to fill the array instead of a loop? How would the time complexity change?"
Practice
Solution
Step 1: Understand TypeScript array syntax
In TypeScript, arrays are declared using the type followed by square brackets, e.g., number[].Step 2: Check each option's syntax
const numbers: number[] = [1, 2, 3]; correctly declares an array of numbers. Options A, B, and D use incorrect syntax for arrays.Final Answer:
const numbers: number[] = [1, 2, 3]; -> Option AQuick Check:
Array declaration with type[] = correct syntax [OK]
- Using type without [] for arrays
- Using parentheses instead of square brackets
- Using curly braces instead of square brackets
Solution
Step 1: Identify correct type annotation for empty string array
To declare an empty array of strings, use string[] as the type and assign an empty array [].Step 2: Validate each option
let names: string[] = []; correctly uses let names: string[] = []. Others have syntax errors or wrong type placement.Final Answer:
let names: string[] = []; -> Option CQuick Check:
Empty array of strings = string[] = correct [OK]
- Omitting [] after type for arrays
- Assigning type incorrectly after =
- Using invalid syntax like let names = string[];
const arr: number[] = [10, 20, 30]; arr[1] = 50; console.log(arr);
Solution
Step 1: Understand array element assignment
Arrays allow changing elements by index. arr[1] changes the second element.Step 2: Apply the assignment and print
arr[1] = 50 changes the second element from 20 to 50. Printing arr shows [10, 50, 30].Final Answer:
[10, 50, 30] -> Option BQuick Check:
Index assignment updates array element [OK]
- Thinking arrays are immutable
- Confusing index 1 with index 0
- Expecting syntax error on assignment
let nums: number[] = (1, 2, 3);
Solution
Step 1: Check array initialization syntax
Arrays must be initialized with square brackets []. Parentheses () create a comma expression, not an array.Step 2: Identify the error
Using (1, 2, 3) does not create an array but evaluates to 3. This is incorrect for array initialization.Final Answer:
Using parentheses instead of square brackets for array initialization -> Option DQuick Check:
Arrays use [] not () for initialization [OK]
- Confusing parentheses with brackets
- Ignoring type annotations
- Thinking let vs const causes error here
Solution
Step 1: Understand 2D array declaration in TypeScript
2D arrays are arrays of arrays, so type is number[][].Step 2: Check initialization for 3 rows and 2 columns
const matrix: number[][] = [[0, 0], [0, 0], [0, 0]]; correctly initializes 3 arrays each with 2 zeros. const matrix: number[3][2] = [[0, 0], [0, 0], [0, 0]]; uses invalid syntax. const matrix: number[] = [0, 0, 0, 0, 0, 0]; is 1D array. const matrix = new Array(3, 2).fill(0); creates array with 2 elements, not 2D.Final Answer:
const matrix: number[][] = [[0, 0], [0, 0], [0, 0]]; -> Option AQuick Check:
2D array = array of arrays with type number[][] [OK]
- Using number[3][2] which is invalid in TypeScript
- Confusing 1D and 2D array initialization
- Using new Array with multiple arguments incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
