Imagine you have a list of names and you want to find if a particular name exists quickly. Which data structure helps you find the name fastest?
Think about which structure lets you check membership without looking at every item.
A hash set uses a method called hashing to find if an item exists very quickly, usually in constant time. Lists, queues, and stacks require checking items one by one, which takes longer as the list grows.
Given these data structures holding 5 items each, how many steps does it take to find the item '3'?
Structures: List [1,2,3,4,5], Set {1,2,3,4,5}, Queue [1,2,3,4,5], Stack [1,2,3,4,5]
Remember how each structure stores and accesses items.
Lists, queues, and stacks require checking items one by one until the target is found. Sets use hashing to find items quickly, usually in one step.
Which data structure generally allows the fastest insertion of a new item?
Think about whether items need to be moved or shifted when inserting.
Linked lists allow fast insertion by changing pointers without moving other items. Arrays may require shifting if full. Binary search trees and hash tables have overhead for balancing or hashing.
This data structure allows fast search, insertion, and deletion on average, but can degrade to slow operations if poorly balanced. Which is it?
Think about a tree that keeps items sorted but can become unbalanced.
A binary search tree offers fast average operations but can become slow if it becomes unbalanced, turning into a linked list in the worst case.
You are building a phone book app that needs to store thousands of contacts and quickly find a contact by name. Which data structure should you use for the contact names to ensure fast search and efficient memory use?
Consider if sorting is important and how fast you need to find contacts.
A balanced binary search tree keeps contacts sorted and allows fast search, insertion, and deletion in logarithmic time. Hash tables are fast but do not keep order, which may be important for a phone book.