Data Structures: The Containers Behind Fast Apps
Arrays, lists, hash maps, stacks, queues, trees, and graphs explained as practical containers for real apps.
Programs handle data: names, prices, appointments, messages, user IDs, documents, files, permissions, and relationships.
A data structure is the container you choose for that data.
The container matters because it decides what is easy, what is slow, and what becomes painful later.
Think about a kitchen. You can throw every spice into one cardboard box, or you can put each spice into a labeled jar. Both choices "store" spices. But when someone asks for salt, the second kitchen wins immediately.
That is data structures.
The practical question
Do not start by asking, "Which data structure should I memorize?"
Ask:
- Do I need to find one item quickly?
- Do I need to keep order?
- Do I need to insert or remove items often?
- Do I need to represent hierarchy?
- Do I need to represent many-to-many relationships?
The answer points to the container.
Array and list: lining things up
In practice, "array" and "list" are often mixed together. JavaScript's Array, Python's list, and Java's ArrayList are called lists but are actually dynamic arrays. In CS textbooks, however, "list" usually means a linked list.
The CS distinction
An array is a row of numbered lockers in contiguous memory.
- Need locker 23? Open it immediately. Index access is O(1).
- Inserting in the middle shifts everything after it.
- Size is often fixed.
A linked list is people holding hands in a line.
- Inserting in the middle only changes two hand-holds. Insert/delete is O(1) once you find the spot, but finding the spot is O(n).
- Finding the 23rd person requires counting from the front.
- Size grows freely.
| View | Array | Linked list |
|---|---|---|
| Memory layout | Contiguous block | Scattered nodes + pointers |
| Index access | O(1), open directly | O(n), count from front |
| Middle insert/delete | O(n), must shift | O(1), relink neighbors |
| Size | Usually fixed | Grows freely |
| Memory use | Compact and predictable | Extra pointers per node |
What languages actually give you
| Language | Name | What it really is |
|---|---|---|
| JavaScript | Array | Dynamic array |
| Python | list | Dynamic array |
| Java | ArrayList | Dynamic array |
| Java | LinkedList | Linked list |
| C++ | std::vector | Dynamic array |
| C++ | std::list | Linked list |
When to use which
Use an array / dynamic array when
- You need fast access by index.
- You mostly append or remove from the end.
- You want contiguous, predictable memory.
- Examples: search results, product lists, table rows, sorted data.
Use a linked list when
- You insert and delete in the middle very often.
- You iterate more than you look up by position.
- The size changes unpredictably.
- Examples: queue/stack implementations, undo history, linked memory blocks.
In everyday app work, you often meet arrays as:
- A list of products.
- Search results.
- Messages in a chat.
- Rows in a table.
- Steps in a workflow.
Hash map: labeled lockers
A hash map is one of the most useful structures in practical programming.
Imagine lockers with labels instead of numbers:
user_123cart_totalappointment_2026_06_25feature_flag_checkout
If you know the label, the value can be found almost immediately.
Hash maps show up everywhere:
- Finding a user by ID.
- Caching an API response.
- Counting how many times each word appears.
- Storing settings by name.
- Grouping records by status.
Stack and queue: pile or line
A stack is a pile of books. The last book placed on top is the first one you remove.
This is called LIFO: last in, first out.
Stacks appear in:
- Browser back buttons.
- Undo history.
- Function calls.
- Nested parsing.
A queue is a cafe line. The first person who arrives is the first person served.
This is called FIFO: first in, first out.
Queues appear in:
- Email sending.
- Background jobs.
- Print jobs.
- Message processing.
- AI tasks waiting to be processed.
Tree and graph: branches and networks
A tree is a hierarchy.
Folders inside folders. Comments with replies. A company org chart. A menu with submenus.
Trees are useful when each item has a parent and children.
A graph is a network.
Social friends. Subway stations. Web pages linking to other pages. Products related to other products.
Graphs are useful when relationships are not simply top-down. One thing can connect to many things in many directions.
Quick comparison
| Structure | Everyday metaphor | Good at | Weak at | Common app use |
|---|---|---|---|---|
| Array | Numbered lockers | Fast access by position | Middle insert/delete | Ordered items, table rows |
| List | People in a line | Flexible growth | Direct position access | Changing sequences |
| Hash map | Labeled lockers | Fast lookup by key | Guaranteed order | User lookup, cache, settings |
| Stack | Pile of books | Last item first | Middle access | Undo, back button |
| Queue | Cafe line | First item first | Middle access | Jobs, messages, task processing |
| Tree | Folder structure | Hierarchy | Cross-links | Menus, comments, files |
| Graph | Subway map | Complex connections | Search cost | Social, routing, recommendations |
How this appears in AI-built apps
AI often writes code that is correct for the sample data in the prompt. That sample data is usually tiny.
Watch for:
-
Arrays used everywhere Using arrays for fast search, deduplication, or membership checks when a different structure would be better.
-
Repeated
array.find()Doing linear scans through a list to find data by ID, instead of usingMapor an object dictionary. -
Deduplication with lists Calling
includes()repeatedly to check for existing values, whenSetwould be simpler and faster. -
Deeply nested relationship data Storing user → posts → comments → likes as one deeply nested object, making updates and lookups complex.
-
Unnormalized state Copying the same data to multiple places so that updating one copy leaves stale data elsewhere.
-
Treating tree structures like lists Scanning all items linearly for hierarchical data like categories, menus, org charts, or comment threads.
-
Treating graph problems like trees Recursing through friend relationships, recommendations, dependencies, or workflows without a visited set.
-
Using
array.shift()where a queue is needed Processing job queues, notifications, or background jobs witharray.shift(), which gets slower as data grows. -
Recursion without stack/queue awareness Handling deep trees or workflows only with recursion, risking stack overflow.
-
Sorting the whole list for top-k Sorting the entire list every time just to find the most important task, latest event, or top score item.
-
Plain object cache without LRU structure Caching data without size limits, expiration, or eviction policies.
-
No index-like structure Recomputing search, filter, groupBy, and count from the original list every time.
-
Direct mutation of nested objects Mutating deep objects directly, which confuses React state detection, undo/redo, and diff calculation.
-
One giant object for large data Managing a single massive object where partial updates become hard and small changes trigger full clones or re-renders.
-
Data access pattern mismatched with structure Using only basic lists and objects without considering read/write ratio, ordering, or lookup speed.
Frequently asked questions
What is a data structure?
A data structure is the shape you give to information so your program can store, find, change, and connect it efficiently.
Array vs list: which should I use?
Use an array or dynamic array when you need fast access by position and mostly append or remove from the end. Use a linked list only when frequent insertions or deletions in the middle are a real bottleneck.
When should I use a hash map?
Use a hash map when you need to look up values by a unique key almost instantly. Examples include user profiles by ID, cached API responses, and settings by name.
What is the difference between a stack and a queue?
A stack is last-in-first-out, like a pile of books. A queue is first-in-first-out, like a line at a cafe.
When should I use a tree instead of a list?
Use a tree when your data has a hierarchy with parents and children, such as folders, comments with replies, or organization charts.
The takeaway
Data structures are not academic trivia. They are the shape of your product’s memory.
Pick the right container, and the code feels obvious. Pick the wrong one, and every feature becomes a fight.
About the Author

Jaehee Song
Enterprise data platform architect with 20+ years of experience building data systems for Fortune 500 companies. AI development educator who has taught vibe coding and AI development to hundreds of students. Founder of Seattle Partners, helping Korean technology startups navigate the US market.