Compare static and dynamic data structures
Compare static and dynamic data structures
In this lesson
- Describing static data structures (arrays, Python tuples) and dynamic data structures (ArrayList, Python lists)
- Explaining how a dynamic list grows in memory
- Comparing the two in flexibility, memory use and speed
- Choosing a suitable structure for a given situation
LearnTwo kinds of data structure
CodeA fixed size and a growing list
tuple length: 3 cannot add a 4th score to the tuple list size: 4
The array (or tuple) cannot take a fourth item; the list grows as items are added.
LearnHow a dynamic list grows
LabSeats and snacks
Sell tickets for a concert with six seats, and take snack orders in a list that grows.
Static: concert seats
seats = [None] * 6 # used as a fixed-size arrayPlaces in memory: 6 · used: 0
Dynamic: snack orders
orders = []Items: 0 · places allocated: 4 · times grown: 0 · items copied so far: 0
Python has no built-in fixed-size array; a tuple has a fixed size but its items cannot be changed at all. In this simplified model the list doubles its storage when full; Java’s ArrayList grows by about 1.5 times and Python lists by a smaller amount, but the idea is the same. To complete this activity, try to sell more tickets than there are seats, and add enough orders for the list to grow twice.
LearnComparing static and dynamic structures
| Static (array, tuple) | Dynamic (ArrayList, Python list) | |
|---|---|---|
| Size | Fixed when created; must be known (or estimated) in advance | Grows and shrinks as needed; the size need not be known |
| Memory | Reserves space for every element, even unused ones; no extra memory beyond that | Uses memory roughly in line with the number of items, plus spare capacity (or a reference per node) |
| Speed | Direct access by index; never pays for resizing | Index access is also direct for array-based lists, but growing means copying items; linked structures must be followed item by item |
| Risk | Cannot hold more than its size: extra items are rejected (e.g. an index error) | Can keep growing until memory runs out |
CheckQuick check
Which of these is a similarity between a Java array and an ArrayList (or a Python tuple and a list)?
CheckStatic or dynamic?
Which kind of structure suits each set of data better?
- The 64 squares of a chessboard
- Messages arriving in a chat
- The lessons in each of the 5 periods of a school day
- The songs a user adds to a playlist
- The names of the players who join an open online game
- The red, green and blue values of one pixel
CheckQuick check
A program creates an array of 1,000 elements to store sensor readings, but on most days only 40 readings arrive. What is the main disadvantage?
ExamWorked exam answer
A program stores the goals scored by each of the 11 players in a football team during a match. Explain whether a static or a dynamic data structure is more suitable.
[3]ReviewMistakes examiners see
Saying a dynamic structure is always better. If the size is known and fixed, a static structure is simpler and wastes nothing.
Claiming that array-based lists cannot be accessed by index. ArrayList and Python lists support direct index access; it is linked structures that must be followed item by item.
Answering a “Compare” question with differences. Compare asks for similarities, referring to both items throughout; “Compare and contrast” asks for similarities and differences.
Link each point to the scenario: is the number of items known? Does it change? How often are items accessed?
Exam practiceExam-style questions
3 questions · 11 marks. Answer each one first, then mark it yourself against the markscheme. Your answers are saved in this browser only.
An online bookshop program keeps each customer’s basket of books while they shop, and stores the total sales for each of the 12 months of the year.
Explain which kind of data structure is more suitable for storing the books in a customer’s basket.
Compare and contrast storing the 12 monthly sales totals in a static structure (a Java array, or a Python tuple) and in a dynamic structure (an ArrayList, or a Python list). The totals are updated after every sale.
A basket is stored in a dynamic list that starts with room for 5 items and doubles its capacity whenever it is full. A customer adds 12 books, one at a time. State how many times the list grows and how many items are copied in total, and explain why growing by doubling is better than growing by one place at a time.
0 of 3 questions marked. Mark every question to see your total.
Exam-style questions written for Praxis IB in the format of the 2027 specimen papers. They are not IB past-paper questions.