Recall & Review
beginner
What is the time complexity of accessing an element by index in an array?
Accessing an element by its index in an array takes constant time, O(1), because arrays allow direct access to any element using its position.
Click to reveal answer
intermediate
Why does inserting an element at the end of a dynamic array sometimes take more than O(1) time?
Inserting at the end is usually O(1), but when the array is full, it needs to resize (allocate a bigger array and copy elements), which takes O(n) time. This resizing happens occasionally, so average insertion is still O(1) amortized.
Click to reveal answer
beginner
What is the time complexity of inserting an element at the beginning of an array?
Inserting at the beginning requires shifting all existing elements one position to the right, so it takes O(n) time, where n is the number of elements in the array.
Click to reveal answer
beginner
Explain the time complexity of deleting an element from the middle of an array.
Deleting an element from the middle requires shifting all elements after it one position to the left to fill the gap. This shifting takes O(n) time, where n is the number of elements in the array.
Click to reveal answer
intermediate
What is the difference between worst-case and amortized time complexity in array operations?
Worst-case time complexity is the maximum time an operation can take (e.g., resizing an array during insertion). Amortized time averages the cost over many operations, showing that occasional expensive operations don't affect the average much.
Click to reveal answer
What is the time complexity of accessing the 5th element in an array?
✗ Incorrect
Accessing any element by index in an array is done in constant time, O(1).
Which operation on an array typically requires O(n) time?
✗ Incorrect
Inserting at the beginning requires shifting all elements, which takes O(n) time.
Why does resizing a dynamic array take O(n) time?
✗ Incorrect
Resizing involves copying all elements to a new array, which takes O(n) time.
What is the amortized time complexity of inserting elements at the end of a dynamic array?
✗ Incorrect
Although resizing is costly, it happens rarely, so average insertion time is O(1) amortized.
Deleting an element from the middle of an array requires:
✗ Incorrect
Elements after the deleted one must be shifted left to fill the gap.
Describe the time complexities of accessing, inserting, and deleting elements in an array.
Think about how many elements need to be moved for each operation.
You got /4 concepts.
Explain why dynamic arrays sometimes need resizing and how it affects operation time.
Consider what happens when you add more elements than the current space.
You got /4 concepts.