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 at the middle index?
Given the array and insertion code below, what is the printed array after insertion?
DSA Python
arr = [10, 20, 30, 40, 50] mid = len(arr) // 2 arr.insert(mid, 25) print(arr)
Attempts:
2 left
💡 Hint
Remember that insert() adds the element before the given index.
✗ Incorrect
The middle index is 2 (5 // 2). Inserting 25 at index 2 places it before the element 30.
❓ Predict Output
intermediate2:00remaining
What is the output after inserting in an even-length array?
What will be printed after inserting 15 at the middle index of the array below?
DSA Python
arr = [5, 10, 20, 25] mid = len(arr) // 2 arr.insert(mid, 15) print(arr)
Attempts:
2 left
💡 Hint
For even length, middle index is length divided by 2.
✗ Incorrect
Length is 4, middle index is 2. Inserting 15 at index 2 places it before 20.
🔧 Debug
advanced2:00remaining
Why does this insertion code raise an error?
Identify the error in the code below and what error it raises.
DSA Python
arr = [1, 2, 3] mid = len(arr) // 2 arr.insert(mid 4) print(arr)
Attempts:
2 left
💡 Hint
Check the syntax of the insert() method call.
✗ Incorrect
The insert() method requires two arguments separated by a comma. Missing comma causes SyntaxError.
❓ Predict Output
advanced2:00remaining
What is the output after multiple insertions at middle?
What is the final array after these insertions?
DSA Python
arr = [100, 200, 300, 400] mid = (len(arr) + 1) // 2 arr.insert(mid, 150) mid = (len(arr) + 1) // 2 arr.insert(mid, 250) print(arr)
Attempts:
2 left
💡 Hint
Recalculate middle index after each insertion.
✗ Incorrect
First insertion at index 2 adds 150 before 300. Array becomes [100, 200, 150, 300, 400].
New middle index is 3. Insert 250 at index 3 before 300.
Final array: [100, 200, 150, 250, 300, 400].
🧠 Conceptual
expert2:00remaining
How does insertion at middle index affect time complexity?
Consider an array of size n. What is the time complexity of inserting an element at the middle index?
Attempts:
2 left
💡 Hint
Think about what happens to elements after the insertion point.
✗ Incorrect
Inserting in the middle requires shifting all elements after the insertion point one step right, which takes O(n) time.