Challenge - 5 Problems
Array Initialization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Array Initialization with Spread Operator
What is the output of the following TypeScript code after array initialization and modification?
DSA Typescript
const arr1 = [1, 2, 3]; const arr2 = [...arr1]; arr2.push(4); console.log(arr1); console.log(arr2);
Attempts:
2 left
💡 Hint
Think about whether arr2 is a new array or a reference to arr1.
✗ Incorrect
The spread operator creates a shallow copy of arr1 into arr2. Modifying arr2 does not affect arr1.
❓ Predict Output
intermediateArray Initialization with Fixed Size and Default Values
What will be the output of this TypeScript code that initializes an array with a fixed size and default values?
DSA Typescript
const arr = new Array(3).fill(0); arr[1] = 5; console.log(arr);
Attempts:
2 left
💡 Hint
Remember that fill sets all elements to the same value initially.
✗ Incorrect
The array is initialized with three zeros. Then the element at index 1 is changed to 5.
🔧 Debug
advancedIdentify the Error in Array Initialization
What error will this TypeScript code produce when run?
DSA Typescript
const arr: number[] = new Array(-1);Attempts:
2 left
💡 Hint
Array size cannot be negative.
✗ Incorrect
Array constructor throws RangeError if length is negative.
❓ Predict Output
advancedOutput of Nested Array Initialization and Modification
What is the output of this TypeScript code involving nested arrays?
DSA Typescript
const arr = Array(3).fill([0]); arr[0][0] = 5; console.log(arr);
Attempts:
2 left
💡 Hint
Consider whether fill creates separate inner arrays or references the same one.
✗ Incorrect
fill uses the same reference for each element, so changing one changes all.
🧠 Conceptual
expertUnderstanding Array Initialization with map vs fill
Which statement correctly explains the difference between initializing an array with fill and map in TypeScript?
Attempts:
2 left
💡 Hint
Think about how fill and map assign values to array elements.
✗ Incorrect
fill assigns the same reference to all elements; map calls a function to create new objects.
