Big O Gym › Problem

Big O Practice: Shift In Loop

Time and space complexity practice in Python and JavaScript. Concept tested: hidden cost: array shift is O(n).

def double_all(arr):
    result = []
    while arr:
        x = arr.pop(0)
        result.append(x * 2)
    return result

Variables

  • n - length of the input array

Explanation

Looks linear - single while-loop, one element handled per iteration - but `arr.shift()` (and Python `arr.pop(0)`) is **O(n)** because every remaining element shifts left. `n` iterations × O(n) per shift = **O(n²)**. Output list takes **O(n)** space.

More Big O practice problems · Today's daily problem