Generic array syntax in Typescript - Time & Space Complexity
We want to understand how the time it takes to work with generic arrays changes as the array grows.
How does the number of steps grow when we use generic arrays in TypeScript?
Analyze the time complexity of the following code snippet.
function printItems<T>(items: T[]): void {
for (const item of items) {
console.log(item);
}
}
const numbers: number[] = [1, 2, 3, 4, 5];
printItems(numbers);
This code prints each item in a generic array, showing how generic array syntax works in TypeScript.
- Primary operation: Looping through each element of the array.
- How many times: Once for each item in the array (n times).
As the array gets bigger, the number of steps grows directly with the number of items.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 steps (print 10 items) |
| 100 | 100 steps (print 100 items) |
| 1000 | 1000 steps (print 1000 items) |
Pattern observation: The steps increase evenly as the array size grows.
Time Complexity: O(n)
This means the time to print items grows in direct proportion to the number of items in the array.
[X] Wrong: "Using generic arrays makes the code slower because of extra type checks at runtime."
[OK] Correct: TypeScript types are erased during compilation, so generics do not add runtime cost or slow down loops.
Understanding how loops over generic arrays behave helps you explain performance clearly and write efficient code in interviews.
"What if we changed the loop to a nested loop over two generic arrays? How would the time complexity change?"
Practice
Solution
Step 1: Understand generic array syntax
In TypeScript,Array<Type>declares an array holding elements ofType.Step 2: Check each option for correct syntax and type
let nums: Array<number> = [1, 2, 3];usesArray<number>with a number array, which is correct. Others have wrong types or assignments.Final Answer:
let nums: Array<number> = [1, 2, 3]; -> Option BQuick Check:
Generic array syntax =Array<Type>[OK]
- Forgetting to specify the type inside Array<>
- Assigning a non-array value to an array type
- Using Array without type parameter
Solution
Step 1: Recall shorthand array syntax
In TypeScript,Type[]declares an array ofType.Step 2: Validate each option
let names: string[] = ['Alice', 'Bob'];correctly usesstring[]for an array of strings. Others are incorrect or incomplete.Final Answer:
let names: string[] = ['Alice', 'Bob']; -> Option AQuick Check:
Shorthand array syntax = Type[] [OK]
- Omitting the type inside Array<>
- Using tuple syntax instead of array
- Assigning array to a non-array type
function firstElement<T>(arr: Array<T>): T | undefined {
return arr[0];
}
const nums: number[] = [10, 20, 30];
console.log(firstElement(nums));Solution
Step 1: Understand the generic function behavior
The functionfirstElementreturns the first element of the array orundefinedif empty.Step 2: Apply function to given array
The arraynumshas elements [10, 20, 30], soarr[0]is 10.Final Answer:
10 -> Option CQuick Check:
firstElement([10,20,30]) = 10 [OK]
- Assuming function returns last element
- Confusing undefined with zero
- Thinking generic causes error
let fruits: Array<string> = ['apple', 'banana', 5];
Solution
Step 1: Check array element types
The array is declared to hold strings only.Step 2: Identify type mismatch
The element5is a number, which violates the string type constraint.Final Answer:
The array contains a number instead of a string -> Option AQuick Check:
Array<string> must hold only strings [OK]
- Mixing types inside a typed array
- Ignoring type errors in arrays
- Confusing syntax errors with type errors
Solution
Step 1: Define generic parameter and array type
The function uses a generic typeTand takes an array ofTelements.Step 2: Ensure return type matches input array type
The function returns an array ofT, soArray<T>is correct for both parameter and return type.Final Answer:
function repeatArray<T>(arr: Array<T>): Array<T> -> Option DQuick Check:
Generic function array syntax = Array<T> [OK]
- Omitting generic type parameter <T>
- Using non-generic Array without type
- Incorrect parameter or return types
Ready to prove it?
Pass the test and this pattern moves unknown
