Python Tuple & Set Interview Questions (Top 50+)

Tuples and sets are important data structures in Python. They are commonly asked in coding interviews to test your understanding of immutability, uniqueness, and data handling.

In this tutorial, we will cover frequently asked tuple and set interview questions with examples and explanations.

1. Tuple Basics

Q1. What is a tuple?

A tuple is an ordered, immutable collection of elements.

Q2. How to create a tuple?

Python
Create tuple
t = (1, 2, 3)

Q3. Difference between list and tuple?

List is mutable, tuple is immutable.

Q4. Can tuple contain different data types?

Yes, tuples can store mixed data types.

2. Tuple Coding Questions

Q5. Access tuple elements

Python
Access element
t = (10, 20, 30)
print(t[1])

Q6. Convert tuple to list

Python
Tuple to list
t = (1,2,3)
print(list(t))

Q7. Count elements

Python
Count
t = (1,2,2,3)
print(t.count(2))

Q8. Find index

Python
Index
t = (1,2,3)
print(t.index(2))

3. Set Basics

Q9. What is a set?

A set is an unordered collection of unique elements.

Q10. How to create a set?

Python
Create set
s = {1, 2, 3}

Q11. Can sets contain duplicates?

No, sets automatically remove duplicates.

4. Set Coding Questions

Q12. Add elements

Python
Add element
s = {1,2}
s.add(3)
print(s)

Q13. Remove elements

Python
Remove element
s.remove(2)

Q14. Union of sets

Python
Union
print({1,2} | {2,3})

Q15. Intersection

Python
Intersection
print({1,2} & {2,3})

5. Advanced Questions

Q16. Remove duplicates from list using set

Python
Remove duplicates
lst = [1,2,2,3]
print(list(set(lst)))

Q17. Check subset

Python
Subset
print({1,2}.issubset({1,2,3}))

Q18. Find symmetric difference

Python
Symmetric difference
print({1,2} ^ {2,3})

6. Coding Problems

Problem 1: Tuple unpacking

Python
Unpacking
a, b, c = (1,2,3)
print(a, b, c)

Problem 2: Find common elements

Python
Common elements
l1 = [1,2,3]
l2 = [2,3,4]
print(list(set(l1) & set(l2)))

Problem 3: Frozen set example

Python
Frozen set
fs = frozenset([1,2,3])
print(fs)

7. Common Mistakes

1. Trying to modify tuples.

2. Using list instead of set for uniqueness.

3. Forgetting sets are unordered.

4. Using mutable elements inside sets.

8. Interview Tips

1. Use tuples for fixed data.

2. Use sets for uniqueness and fast lookup.

3. Practice set operations.

4. Understand immutability.

9. Practice Questions

1. Find union of multiple sets.

2. Remove duplicates using set.

3. Count elements in tuple.

4. Convert list to tuple.

5. Find difference between sets.

Conclusion

Tuples and sets are important for efficient programming. Understanding their properties helps solve many interview problems easily.

Practice regularly and explore advanced concepts to strengthen your knowledge.