Introduction
A data structure is a specific way of organizing data in memory so that certain operations, like finding an item, adding one, or removing one, can be done efficiently. There is no single best data structure for every situation; each one makes trade-offs, being fast at some operations and slower at others, so choosing the right structure for a problem is one of the most important early decisions a programmer makes.
Cricket analogy: A team manager choosing how to store player fitness records could use a simple ordered squad list for quick lookup by position, or a searchable database for quick lookup by name, and each choice trades off speed for a different kind of query, the same trade-off a data structure makes for different operations.
Explanation
An array (or list) stores elements in a contiguous, ordered sequence, so accessing an element by its position is very fast, but inserting or removing an element in the middle requires shifting every element after it. A stack allows adding and removing only from one end, following last-in-first-out order, which is exactly what is needed for tasks like undo history, where the most recent action should be undone first. A queue allows adding at one end and removing from the other, following first-in-first-out order, matching real-world situations like processing requests in the order they arrived.
Cricket analogy: A batting order is like an array: you can instantly know who bats fifth by position, but inserting a new player at third means shifting everyone below down one spot, exactly like an array's fast indexed access but costly middle insertion.
A map (also called a dictionary or hash map) stores data as key-value pairs and is optimized for extremely fast lookup by key, at the cost of not preserving any particular order of the elements the way an array does. Choosing between these structures comes down to which operation matters most for the program: fast lookup by position favors an array, fast lookup by a unique identifier favors a map, undo-style behavior favors a stack, and fairness-based ordering favors a queue.
Cricket analogy: A player registry looked up by jersey number instantly, regardless of when each player joined the squad, behaves like a map: fast lookup by a unique key, without caring about arrival order, exactly matching a map's trade-off against an array's ordered access.
# Stack (undo history) using a Python list
undo_stack = []
undo_stack.append("typed 'hello'")
undo_stack.append("typed ' world'")
last_action = undo_stack.pop() # "typed ' world'" removed last-in-first-out
# Queue (support tickets) using collections.deque
from collections import deque
ticket_queue = deque()
ticket_queue.append("ticket #1")
ticket_queue.append("ticket #2")
next_ticket = ticket_queue.popleft() # "ticket #1" removed first-in-first-out
# Map (lookup by key) using a Python dict
users_by_id = {"u1": "Alice", "u2": "Bob"}
print(users_by_id["u2"]) # instant lookup by keyKey Takeaways
- Arrays store elements contiguously, giving fast access by position but slow insertion in the middle.
- Stacks follow last-in-first-out order, suited for tasks like undo history.
- Queues follow first-in-first-out order, suited for fair, arrival-order processing.
- Maps store key-value pairs for fast lookup by key, without preserving a particular order.
- There is no universally best data structure; the right choice depends on which operations matter most.
Practice what you learned
1. What is a key trade-off of using an array?
2. Which data structure follows last-in-first-out order?
3. Which data structure is best suited for processing tasks fairly in the order they arrived?
4. What does a map (dictionary) optimize for?
5. Why is there no single 'best' data structure for every situation?
Was this page helpful?
You May Also Like
Pseudocode
How pseudocode expresses an algorithm's logic in structured, language-agnostic plain language before writing real code.
Debugging Basics
How to systematically find and fix defects in code using reproduction, isolation, print statements, and debuggers.
What Is a Framework
How a software framework provides reusable structure and calls your code, unlike a library which your code calls directly.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics