0
0
DSA Pythonprogramming~5 mins

Array Deletion at End in DSA Python - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Array Deletion at End
O(1)
Understanding Time Complexity

We want to understand how fast it is to remove an item from the end of an array.

How does the time needed change as the array gets bigger?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.

def delete_at_end(arr):
    if len(arr) == 0:
        return None
    return arr.pop()

This code removes the last item from the array if it exists.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Removing the last element using pop()
  • How many times: Exactly once per call, no loops or recursion
How Execution Grows With Input

Removing the last item takes the same amount of time no matter how big the array is.

Input Size (n)Approx. Operations
101
1001
10001

Pattern observation: The time stays constant as the array grows.

Final Time Complexity

Time Complexity: O(1)

This means removing the last item takes the same short time no matter how big the array is.

Common Mistake

[X] Wrong: "Removing the last item takes longer if the array is bigger because it has to shift elements."

[OK] Correct: When deleting at the end, no elements need to move. Only the last item is removed, so time stays the same.

Interview Connect

Knowing that deleting at the end is very fast helps you choose the right data structure and method in real problems.

Self-Check

"What if we changed deletion to remove the first item instead? How would the time complexity change?"