227 views
0 0 votes

In Python, the $\verb|list|$ and $\verb|tuple|$ types handle memory allocation differently. Given the following operations:

import sys

L = [1, 2, 3]
T = (1, 2, 3)

L.append(4)
# Assume T is recreated as T = (1, 2, 3, 4)


Which of the following statements regarding Python's internal implementation is TRUE?

  1. LISTS ARE STORED IN A CONTIGUOUS BLOCK OF MEMORY, WHILE TUPLES USE LINKED LISTS TO MAINTAIN IMMUTABILITY.
     
  2. APPENDING TO A LIST HAS A WORST-CASE TIME COMPLEXITY OF $O(1)$ BECAUSE MEMORY IS ALWAYS PRE-ALLOCATED.
     
  3. TUPLES ARE GENERALLY MORE MEMORY-EFFICIENT THAN LISTS BECAUSE THEY DO NOT REQUIRE OVER-ALLOCATION FOR FUTURE GROWTH.
     
  4. THE IS OPERATOR WILL ALWAYS RETURN $\verb|TRUE|$ FOR TWO SEPARATE TUPLE DEFINITIONS CONTAINING THE SAME INTEGERS (E.G., $\verb|(1,2) IS (1,2)| )$.

1 Answer

0 0 votes

Correct Answer: C

 

A. False  

Both lists and tuples in Python are implemented using contiguous memory (array-like structures).

Tuples are not linked lists. Immutability of tuples is a design property, not achieved via linked list structure.

Example:

L = [1,2,3]

T = (1,2,3)

Both are stored as arrays of references, not linked nodes.

Hence the statement is incorrect.

 

B. False

Appending to a list is amortized O(1) due to over-allocation of memory.

Appending to a list is amortized O(1) because most insertions happen in constant time when there is already extra space available.

Python uses over-allocation, meaning it increases the list size by more than needed during resizing, so future appends don’t require copying every time.

Occasionally, when the list becomes full, it resizes and copies elements (O(n)), but this happens rarely.

Because these costly operations are spread over many cheap ones, the average (amortized) time per append remains O(1).

In short, most insertions are O(1), and only occasional resizing takes O(n), so the amortized cost per insertion is O(1).


However, when the allocated space is exhausted, resizing occurs, requiring copying of all elements, which takes O(n) time.

Therefore, worst-case time complexity is not O(1).

So, appending to a list is amortized O(1), but worst-case time complexity is O(n) due to resizing.

Example:

L = [1,2,3]

# When capacity is full:

L.append(4) # may trigger resizing → O(n)

 

C. True

Lists allocate extra memory (over-allocation) to allow efficient appends, which increases memory usage.

Tuples, being immutable, do not require such extra space and are stored more compactly.

Hence, tuples are generally more memory-efficient than lists.

 

D. False  

The "is" operator checks whether two variables refer to the same object in memory, not whether their values are equal.

Two tuples with identical contents may or may not refer to the same object, depending on Python's internal optimizations.

Thus, it is not always true.

Example:

a = (1,2)

b = (1,2)

print(a is b) # not guaranteed True

print(a == b) # always True

Special Case:

(1,2) is ((1,2)) # True

Reason: Extra parentheses do not create a new tuple; it refers to the same object.

Key Trick:

"is" → same memory (identity)

"==" → same value (equality)

z

• edited by
Answer:
Position:
Show:

Related questions

3 3 votes
1 1 answer
251
251 views
GO Classes asked Feb 6
251 views
Consider the following pseudocode for a function $\verb|process(n)| :$def process(n): count = 0 i = n while i 1: j = 1 while j < n: ...
0 0 votes
1 1 answer
241
241 views
GO Classes asked Feb 6
241 views
Which of the following statement(s) is/are TRUE regarding searching and sorting?IF AN INPUT ARRAY IS ALREADY SORTED, BINARY SEARCH TAKES $O(\log n)$ TIME, BUT SEARCHING F...
0 0 votes
1 1 answer
193
193 views
GO Classes asked Feb 6
193 views
Consider a hash table with $M=11$ slots, using Linear Probing with the hash function $h(k, i)=(k+i) \bmod 11$, where $i \in\{0,1, \ldots, 10\}$ is the probe number. The f...
1 1 vote
1 1 answer
181
181 views
GO Classes asked Feb 6
181 views
def create_multipliers(): return [lambda x: i * x for i in range(4)] multipliers = create_multipliers() result = [m(2) for m in multipliers] print(result)What is ...