Recall & Review
beginner
What is the time complexity of accessing an element at a specific index in an array?
Accessing an element at a specific index in an array takes O(1) time because arrays allow direct access using the index.
Click to reveal answer
beginner
How do you update the value at index 3 in an array named
arr to 10 in Python?You update the value by assigning the new value directly: <br>
arr[3] = 10Click to reveal answer
beginner
What happens if you try to access an index that is out of the array's range?
Accessing an index outside the array's range causes an IndexError in Python, meaning the index is invalid.
Click to reveal answer
intermediate
Explain why arrays are good for fast access but not always for fast insertion or deletion.
Arrays allow fast access because elements are stored contiguously and can be accessed by index directly. But insertion or deletion in the middle requires shifting elements, which takes O(n) time.
Click to reveal answer
beginner
Show the Python code to access and update the 2nd element of an array
arr and print the updated array.arr = [5, 8, 12, 20]
print("Before update:", arr)
arr[1] = 15 # Update 2nd element (index 1)
print("After update:", arr)
Click to reveal answer
What is the index of the first element in a Python array (list)?
✗ Incorrect
Python arrays (lists) are zero-indexed, so the first element is at index 0.
What happens if you try to update an element at an index that does not exist in the array?
✗ Incorrect
Trying to update an element at an invalid index raises an IndexError in Python.
Which operation is fastest in an array?
✗ Incorrect
Accessing by index is O(1), faster than insertion, deletion, or searching.
How do you update the 4th element in an array named
arr to 100 in Python?✗ Incorrect
Indexing starts at 0, so the 4th element is at index 3.
If
arr = [1, 2, 3], what is the output after arr[1] = 5?✗ Incorrect
The element at index 1 (second element) is updated to 5.
Describe how to access and update an element at a specific index in an array.
Think about how you pick and change a book on a shelf by its position.
You got /4 concepts.
Explain why accessing an element by index in an array is fast but inserting in the middle is slower.
Imagine a row of chairs where you can sit directly but adding a chair in the middle means moving others.
You got /4 concepts.