Day 7 & 8 - Python Quiz (Lists, Tuples & Sets)

  • List: Ordered, mutable, allows duplicates
  • Tuple: Ordered, immutable, allows duplicates
  • Set: Unordered, mutable, stores unique values only
1. Which data structure is mutable?
Lists can be modified after creation.
2. Which data structure is immutable?
Tuples cannot be changed once created.
3. Which does NOT allow duplicate values?
Sets store only unique elements.
4. Which data structure is unordered?
Sets do not maintain insertion order.
5. Which uses square brackets?
Lists are defined using square brackets [].
6. Which supports indexing?
Lists and tuples support indexing, sets do not.
7. Which method adds element at end of list?
append() adds element at the end of a list.
8. Which list method removes element by index?
pop() removes element using index.
9. Which tuple method counts occurrences?
count() returns frequency of an element.
10. Which set method adds one element?
add() inserts a single element into set.
11. Which set method adds multiple elements?
update() adds multiple values.
12. Which removes element without error if missing?
discard() never raises error.
13. Which set operation finds common elements?
intersection() returns common values.
14. Which combines two sets?
union() merges both sets.
15. Best structure to remove duplicates from list?
Set automatically removes duplicates.
16. Which is faster for membership testing?
Sets use hashing for fast lookup.
17. Which is best for fixed coordinates?
Tuples protect fixed data.
18. Which data structure can store mixed types?
Lists can store mixed data.
19. Which supports slicing?
Lists and tuples support slicing.
20. Which structure is ideal for uniqueness + speed?
Sets ensure uniqueness and fast lookup.

Advanced Notes (Read Carefully)

1. Time Complexity Insight (Core of DSA)

Understanding time complexity is what separates a beginner from an advanced programmer. Choosing the wrong data structure can make a solution fail even if the logic is correct.

  • List membership (x in list) → O(n)
    Python checks each element one by one.
  • Tuple membership (x in tuple) → O(n)
    Same behavior as list, but immutable.
  • Set membership (x in set) → O(1) (average case)
    Uses hashing internally, extremely fast.
  • Dictionary key lookup (key in dict) → O(1)
    This is why dictionaries are called hash maps.

2. Mutability, Immutability & Safety

Mutability defines whether data can be changed after creation. Understanding this helps avoid bugs and accidental data corruption.

  • Lists (Mutable)
    Lists can grow, shrink, and change values.
    lst = [1, 2, 3]
    lst[0] = 10
    
  • Tuples (Immutable)
    Once created, tuples cannot be modified.
    t = (1, 2, 3)
    # t[0] = 10   ❌ Error
    
  • Sets
    Mutable but unordered. Used mainly for uniqueness and speed.

3. Memory & Performance Considerations

  • Tuples consume less memory than lists
  • Lists consume moderate memory
  • Sets use more memory but provide very fast lookup
  • Dictionaries use more memory but give fastest key-based access

Advanced Insight:
High-performance systems trade memory for speed using sets and dictionaries.

4. Real DSA Usage Mapping

  • Lists are used to represent:
    • Arrays
    • Stacks (append + pop)
    • Queues (with deque)
  • Tuples are used for:
    • Coordinates like (x, y)
    • Fixed configuration values
    • Dictionary keys (hashable)
  • Sets are used for:
    • Duplicate detection
    • Visited tracking in graphs
    • Sliding window optimization
    • Fast membership checks

5. Conversion Techniques (Very Important)

Advanced programmers frequently convert between data structures to optimize both performance and safety.

lst = [1, 2, 2, 3, 4]

set(lst)           # remove duplicates
list(set(lst))     # convert back to list
tuple(lst)         # make data immutable

6. Hashing Concept (Behind the Scenes)

Sets and dictionaries work using hashing.

  • Each element or key is converted into a hash value
  • The hash decides the storage location
  • This enables O(1) average lookup time

Important Rule:
Only immutable objects (int, str, tuple) can be dictionary keys.

7. Interview Thinking Rules

When reading a problem statement:

  • If it mentions duplicates → think Set
  • If it mentions frequency or count → think Dictionary
  • If data is fixed → think Tuple
  • If data is dynamic → think List

8. Common Advanced Mistakes

  • Using list instead of set for membership testing
  • Expecting order from sets
  • Using mutable objects as dictionary keys
  • Overusing lists where dictionary is required

9. Real-World Programming Insight

Modern backend frameworks like Django and FastAPI internally rely on:

  • Dictionaries for request and response data
  • Lists for collections of records
  • Tuples for fixed configuration values

Most difficult problems are not hard because of logic, but because of choosing the wrong data structure.

If you deeply understand these notes, you are already thinking like an advanced Python and DSA developer.

Post a Comment

0 Comments