Interview Questions
These questions are frequently asked in FAANG-level interviews and test your deep understanding of tuples, including immutability, performance, unpacking, and tricky behaviors.
Each question includes a clear explanation and correct solution.
Question 1: What is a Tuple and Why Is It Used?
Answer
A tuple is an ordered, immutable collection in Python.
It is used when data should not be modified, improving safety and performance.
Example
python
data = (10, 20, 30)
Key reasons to use tuples:
- Faster than lists
- Hashable (can be dictionary keys)
- Protects data from accidental modification
Question 2: Difference Between List and Tuple?
Answer
| Feature | List | Tuple |
|---|---|---|
| Mutable | Yes | No |
| Syntax | [] | () |
| Performance | Slower | Faster |
| Hashable | No | Yes (if elements are immutable) |
Example
python
t = (1, 2, 3)
l = [1, 2, 3]
Question 3: How Do You Create a Single-Element Tuple?
Answer
A comma is mandatory.
python
t = (10,)
print(type(t))
Without comma:
python
t = (10)
print(type(t)) # int
Question 4: Why Are Tuples Immutable?
Answer
Tuples are immutable to:
- Ensure data integrity
- Improve performance
- Enable safe usage as dictionary keys
This makes tuples suitable for fixed configuration data.
Question 5: Can a Tuple Contain Mutable Objects?
Answer
Yes.
python
t = (1, [2, 3], 4)
t[1].append(99)
print(t)
Tuple reference does not change, but mutable objects inside can change.
Question 6: How Do You Modify a Tuple?
Answer
Indirectly by converting to a list.
python
t = (1, 2, 3)
temp = list(t)
temp[0] = 10
t = tuple(temp)
print(t)
Question 7: Tuple Packing and Unpacking?
Answer
Packing
python
t = 10, 20, 30
Unpacking
python
a, b, c = t
Question 8: Extended Tuple Unpacking
Answer
python
t = (1, 2, 3, 4, 5)
a, *b, c = t
print(a)
print(b)
print(c)
b becomes a list.Question 9: Swap Two Variables Without Temp Variable
Answer
python
a = 10
b = 20
a, b = b, a
This is powered by tuple unpacking.
Question 10: Why Are Tuples Faster Than Lists?
Answer
Because:
- Tuples are immutable
- Fixed memory allocation
- Less overhead than lists
FAANG interviews often test this conceptual clarity.
Question 11: Can Tuples Be Dictionary Keys?
Answer
Yes, if all elements are immutable.
python
data = {(1, 2): "value"}
print(data[(1, 2)])
This is not allowed:
python
# {(1, [2]): "value"} # TypeError
Question 12: How Many Methods Does a Tuple Have?
Answer
Only two.
python
t = (1, 2, 2, 3)
print(t.count(2))
print(t.index(3))
Question 13: Predict the Output (Tricky)
python
t = (1, 2)
t += (3,)
print(t)
Answer
text
(1, 2, 3)
A new tuple is created.
Question 14: Tuple vs NamedTuple (Advanced)
Answer
namedtuple provides readability with named fields.python
from collections import namedtuple
Person = namedtuple("Person", ["name", "age"])
p = Person("Jayesh", 25)
print(p.name)
print(p.age)
Used in clean architecture and system design.
Question 15: Tuple in Function Return (FAANG Favorite)
Question
How does Python return multiple values?
Answer
python
def calc(a, b):
return a + b, a - b
result = calc(10, 5)
print(result)
Python returns a tuple.
Question 16: Tuple Memory Comparison
Answer
python
import sys
list_data = [1, 2, 3]
tuple_data = (1, 2, 3)
print(sys.getsizeof(list_data))
print(sys.getsizeof(tuple_data))
Tuples consume less memory.
Question 17: Tuple vs Set (Common Trap)
Answer
| Tuple | Set |
|---|---|
| Ordered | Unordered |
| Allows duplicates | No duplicates |
| Immutable | Mutable |
| Index-based access | No indexing |
Question 18: Tuple Slicing Output Type
python
t = (1, 2, 3)
x = t[1:]
print(type(x))
Answer
tupleQuestion 19: Tuple Comparison
python
print((1, 2) > (1, 1))
Answer
text
True
Python compares element by element.
Question 20: When NOT to Use Tuples?
Answer
Avoid tuples when:
- Data needs frequent updates
- Size changes dynamically
- Complex modifications are required
Use lists instead.
Interview Takeaways
- Tuples are immutable and faster
- Widely used in FAANG-level code
- Common in function returns
- Safe for fixed and structured data
- Powerful when combined with unpacking
- Frequently tested in tricky scenarios
Mastering tuples shows strong Python fundamentals, which FAANG interviewers value highly.