0
0
DSA Pythonprogramming~20 mins

Array Insertion at Middle Index in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Insertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A[10, 20, 25, 30, 40, 50]
B[10, 20, 30, 25, 40, 50]
C[10, 20, 30, 40, 25, 50]
D[25, 10, 20, 30, 40, 50]
Attempts:
2 left
💡 Hint
Remember that insert() adds the element before the given index.
Predict Output
intermediate
2: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)
A[5, 10, 15, 20, 25]
B[5, 15, 10, 20, 25]
C[15, 5, 10, 20, 25]
D[5, 10, 20, 15, 25]
Attempts:
2 left
💡 Hint
For even length, middle index is length divided by 2.
🔧 Debug
advanced
2: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)
ANo error, prints [1, 2, 4, 3]
BTypeError because insert() expects only one argument
CIndexError because mid is out of range
DSyntaxError due to missing comma in insert() arguments
Attempts:
2 left
💡 Hint
Check the syntax of the insert() method call.
Predict Output
advanced
2: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)
A[100, 150, 200, 250, 300, 400]
B[100, 200, 150, 300, 250, 400]
C[100, 200, 150, 250, 300, 400]
D[150, 100, 200, 250, 300, 400]
Attempts:
2 left
💡 Hint
Recalculate middle index after each insertion.
🧠 Conceptual
expert
2: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?
AO(1) because insertion is direct at the middle index
BO(n) because elements after middle must be shifted
CO(log n) because binary search is used to find middle
DO(n^2) because all elements are copied twice
Attempts:
2 left
💡 Hint
Think about what happens to elements after the insertion point.