Challenge - 5 Problems
Array Insertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after inserting elements at the end of the array?
Consider the following TypeScript code that inserts elements at the end of an array. What will be the final state of the array printed?
DSA Typescript
let arr: number[] = [1, 2, 3]; arr.push(4); arr.push(5); console.log(arr);
Attempts:
2 left
💡 Hint
Remember that push adds elements to the end of the array.
✗ Incorrect
The push method adds elements to the end of the array, so after pushing 4 and 5, the array becomes [1, 2, 3, 4, 5].
❓ Predict Output
intermediateWhat is the output after multiple insertions at the end?
What will be printed after running this TypeScript code?
DSA Typescript
const numbers: number[] = []; numbers.push(10); numbers.push(20); numbers.push(30); console.log(numbers);
Attempts:
2 left
💡 Hint
Each push adds one element at the end.
✗ Incorrect
Starting from an empty array, pushing 10, 20, and 30 adds them in order at the end, resulting in [10, 20, 30].
🔧 Debug
advancedIdentify the error in this array insertion code
What error will this TypeScript code produce when trying to insert elements at the end of the array?
DSA Typescript
let arr: number[] = [1, 2, 3]; arr.push(4, 5); arr.push('6'); console.log(arr);
Attempts:
2 left
💡 Hint
Check the type of elements pushed to the array.
✗ Incorrect
The array is typed to hold numbers only. Pushing a string '6' causes a type error in TypeScript.
❓ Predict Output
advancedWhat is the output after inserting elements using length property?
What will be the output of this TypeScript code that inserts elements at the end using the length property?
DSA Typescript
let arr: number[] = [5, 6]; arr[arr.length] = 7; arr[arr.length] = 8; console.log(arr);
Attempts:
2 left
💡 Hint
Using arr.length as index adds elements at the end.
✗ Incorrect
Assigning to arr[arr.length] adds elements at the end of the array, so the final array is [5, 6, 7, 8].
🧠 Conceptual
expertWhy is push preferred over direct length assignment for insertion?
Which of the following is the best reason to prefer using push() over assigning to arr[arr.length] when inserting at the end of an array in TypeScript?
Attempts:
2 left
💡 Hint
Think about safety and automatic updates.
✗ Incorrect
push() method safely adds elements at the end and updates length automatically, reducing errors compared to manual index assignment.
