O(1) constant space
easy 14 minO(1) space means an algorithm uses a fixed amount of extra memory, a few variables, no matter how large the input is.
Check yourself
One question at a time. Each answer explains why.
Question 1 of 10
What does O(1) space complexity mean?
Which technique commonly achieves O(1) space for array problems?
You reverse [1, 2, 3, 4, 5, 6, 7, 8] in place with two pointers and a temp. How much extra space is used?
Which operation is NOT O(1) space?
Why is O(1) space desirable?
find_max scans [3, 7, 2, 9, 1, 8, 4, 6] keeping one best variable and one index. What is its space complexity?
The XOR swap (a ^= b; b ^= a; a ^= b) exchanges two variables using how many extra variables?
Which O(1) space operation modifies the input array?
What is the space complexity of Floyd's cycle detection (tortoise and hare)?
This function sums a list: `def total(a, i=0): return 0 if i == len(a) else a[i] + total(a, i + 1)`. What is its auxiliary space?
Pick an answer to see why.
Not quite. O(1) space means a constant amount of extra memory that does not grow with the input. Processing 10 or 10 million elements uses the same fixed set of variables.
Not quite. Two pointers (left and right, or slow and fast) use just two index variables whatever the array size. Hash maps and DP tables usually take O(n) space, and recursion takes stack space proportional to its depth.
Not quite. Only left, right and temp are stored: three variables for any length. The 4 swaps are time, not space.
Not quite. A hash map stores up to n entries, so it grows with the input: O(n). The others use a fixed number of variables.
Not quite. Constant-space algorithms can handle very large inputs and run on memory-limited devices. They are not always faster and are sometimes harder to write.
Not quite. Two variables, the same for any array length, so O(1) auxiliary space. The array itself is input space and is not counted.
Not quite. None. XOR is its own inverse (x ^ x = 0), so the values are exchanged in place. It breaks if a and b are the same memory cell, and a temp variable is still O(1), so it saves nothing asymptotically.
Not quite. In-place algorithms overwrite the input to avoid allocating new storage. Sum, minimum and count only read it.
Not quite. It keeps only two node references, slow and fast, regardless of the list's length.
Not quite. Each call waits for the next, so n + 1 frames are alive at the deepest point. Recursion depth n means O(n) space. A loop with one running sum would be O(1).
Flashcards
Say your answer, then reveal it.
Card 1 of 10
What does O(1) space mean in one sentence?
The extra memory the algorithm uses stays below a fixed constant, no matter how large the input is.
Say your answer, then reveal it.
Input space vs auxiliary space?
Input space is what the input already occupies. Auxiliary space is the extra memory the algorithm allocates. Space complexity usually quotes auxiliary space.
Say your answer, then reveal it.
How much extra memory does a two-pointer pair sum use?
Two indexes (left and right), plus one integer for the sum. Constant, so O(1).
Say your answer, then reveal it.
What does an in-place reverse cost?
floor(n/2) swaps, so O(n) time, and two indexes plus one temp, so O(1) space.
Say your answer, then reveal it.
Why is arr[::-1] not O(1) space?
The slice builds a brand new list of n elements, so its auxiliary space is O(n).
Say your answer, then reveal it.
Why can recursion break O(1) space?
Each pending call keeps a stack frame. Space equals max depth times frame size, so n levels deep means O(n) space even with no lists.
Say your answer, then reveal it.
How much space does Floyd's cycle detection use?
O(1): two node references, slow (1 step) and fast (2 steps).
Say your answer, then reveal it.
Is Python's list.sort() O(1) space?
No. It sorts in place, but Timsort can use up to about n/2 extra slots while merging. Heap sort is the O(1) space comparison sort.
Say your answer, then reveal it.
What are the three main trade-offs of O(1) space?
It may destroy the input, it often needs sorted input, and it may cost more time than an O(n) space alternative.
Say your answer, then reveal it.
What is the memory hook for O(1) space?
The pocket rule: only use what fits in your pockets. A few pointers and counters, never a backpack-sized copy.
Say your answer, then reveal it.
These cards come back in Today, just before you would forget them.