Study smarter with Fiveable
Get study guides, practice questions, and cheatsheets for all your subjects. Join 500,000+ students with a 96% pass rate.
Data structures are the foundation of everything you'll build as a programmer—they determine how efficiently your code runs and how elegantly it solves problems. You're being tested not just on what each structure is, but on when to use it and why it performs the way it does. Understanding the tradeoffs between structures like arrays vs. linked lists or hash tables vs. trees will show up repeatedly in exam questions, coding challenges, and real-world development.
Think of data structures as tools in a toolbox. A hammer and a screwdriver both connect things, but choosing the wrong one makes your job harder. The same principle applies here: time complexity, memory usage, and access patterns determine which structure fits your problem. Don't just memorize definitions—know what problem each structure solves best and what tradeoffs you're accepting when you choose it.
These structures store elements in a sequence where position matters. The key distinction is whether elements are stored contiguously in memory or linked through references.
Compare: Arrays vs. Linked Lists—both store sequences, but arrays win for access speed ( vs. ) while linked lists win for modification flexibility ( vs. ). If an exam asks about tradeoffs between memory locality and dynamic sizing, this is your go-to comparison.
These structures limit how you interact with data to enforce specific behaviors. The restriction itself is the feature—it guarantees predictable ordering of operations.
push adds to the top, pop removes from the top, both in timeenqueue adds to the back, dequeue removes from the front, both in timeCompare: Stacks vs. Queues—both restrict access to enforce ordering, but LIFO (stacks) handles nested/recursive problems while FIFO (queues) handles sequential processing. When asked about algorithm design, identify whether you need to "go back" (stack) or "process in order" (queue).
These structures optimize for retrieval speed by using keys to locate data. The magic is in the hash function or key comparison that bypasses sequential searching.
dict, Java's HashMap) add features like ordering guarantees or type constraintsunion, intersection, and difference enable powerful data comparisonsCompare: Hash Tables vs. Sets—both use hashing for fast lookup, but hash tables store key-value pairs while sets store only keys (values). Use sets when you only care about membership ("Is X present?") and hash tables when you need to associate data with keys.
These structures represent parent-child relationships. The branching nature enables efficient searching and natural modeling of real-world hierarchies.
Compare: BSTs vs. Heaps—both are tree-based, but BSTs optimize for searching any value () while heaps optimize for accessing the single max/min (). Choose BSTs for general lookup, heaps for priority-based processing.
These structures model connections between entities where relationships are as important as the entities themselves. The flexibility to represent any connection pattern makes graphs uniquely powerful.
Compare: Trees vs. Graphs—trees are actually a restricted type of graph (connected, acyclic, one path between nodes). Use trees when hierarchy is clear; use graphs when relationships are more complex or cyclical.
| Concept | Best Examples |
|---|---|
| random access | Arrays |
| insertion/deletion | Linked Lists, Hash Tables |
| LIFO ordering | Stacks |
| FIFO ordering | Queues |
| Key-value lookup | Hash Tables, Dictionaries |
| Unique element storage | Sets |
| Hierarchical relationships | Trees |
| Priority-based access | Heaps |
| Complex relationships | Graphs |
You need to implement an "undo" feature that reverses the last 10 user actions. Which data structure is most appropriate, and why does its access pattern fit this problem?
Compare arrays and linked lists: under what conditions would you choose a linked list despite its slower access time?
Both hash tables and binary search trees can store key-value pairs. What are the tradeoffs in terms of time complexity and when might you prefer one over the other?
A hospital emergency room needs to process patients by severity rather than arrival time. Which data structure should manage the patient queue, and what property makes it suitable?
Explain why a social network's friend connections would be modeled as a graph rather than a tree. What specific graph characteristics apply here?