Challenge - 5 Problems
Array Insertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output after inserting elements at the end of the array?
Consider the following Python code that inserts elements at the end of an array (list). What will be printed?
DSA Python
arr = [1, 2, 3] arr.append(4) arr.append(5) print(arr)
Attempts:
2 left
💡 Hint
Remember that append adds elements to the end of the list.
✗ Incorrect
The append method adds elements at the end of the list. So after appending 4 and 5, the list becomes [1, 2, 3, 4, 5].
❓ Predict Output
intermediate2:00remaining
What is the final array after multiple insertions at the end?
What will be the output of this code snippet?
DSA Python
arr = [] for i in range(3): arr.append(i * 2) print(arr)
Attempts:
2 left
💡 Hint
Check the values generated by i * 2 for i in range(3).
✗ Incorrect
The loop runs for i = 0, 1, 2. Multiplying by 2 gives 0, 2, 4. These are appended in order.
🔧 Debug
advanced2:00remaining
Identify the error in this array insertion code
What error will this code produce when run?
DSA Python
arr = [1, 2, 3] arr.append(4, 5) print(arr)
Attempts:
2 left
💡 Hint
Check how many arguments append accepts.
✗ Incorrect
The append method accepts only one argument. Passing two causes a TypeError.
🚀 Application
advanced2:00remaining
What is the array after inserting elements using extend?
Given the code below, what will be the final state of the array?
DSA Python
arr = [1, 2] arr.extend([3, 4]) print(arr)
Attempts:
2 left
💡 Hint
Remember that extend adds each element of the iterable individually.
✗ Incorrect
The extend method adds each element of the given list to the end of the original list.
🧠 Conceptual
expert2:00remaining
How many elements are in the array after these insertions?
If we start with an empty array and perform these operations, how many elements will the array have at the end?
arr = []
arr.append(1)
arr.append([2, 3])
arr.extend([4, 5])
Attempts:
2 left
💡 Hint
Remember append adds the whole object as one element, extend adds each element separately.
✗ Incorrect
After append(1), array has 1 element.
append([2, 3]) adds the list as one element, total 2.
extend([4, 5]) adds two elements individually, total 4.