You want to store a collection of unique items and check if an item exists quickly. Which data structure is best?
Think about which structure lets you check if an item is inside without looking at every element.
A set stores unique items and uses a method that lets it check if an item is present very quickly, unlike a list which may need to check each item one by one.
You need to store student names and their grades. Which data structure is better to quickly find a student's grade by name?
Think about which structure lets you find a grade by name without searching through all items.
Dictionaries store data as key-value pairs, so you can get a grade by using the student's name as a key instantly, unlike a list where you might have to check each item.
Given the following operations on an empty stack, what is the final content of the stack?
stack = [] stack.append(10) stack.append(20) stack.pop() stack.append(30) stack.pop() stack.append(40)
Remember that pop() removes the last item added (LIFO).
Step by step: start empty, add 10, add 20, remove 20, add 30, remove 30, add 40. Left with 10 and 40.
You have a data structure that adds items at one end and removes items from the other end, processing items in the order they were added. What is it?
Think about the order items come out compared to the order they went in.
A queue adds items at the back and removes from the front, so items come out in the same order they went in (FIFO).
You are designing a phone book app that stores names and phone numbers. Users often search by name, add new contacts, and delete contacts. Which data structure is best to use internally?
Consider which structure supports fast search, addition, and deletion by key.
Dictionaries allow fast lookup, insertion, and deletion by key, which fits the phone book needs better than lists or stacks.