Choosing the Right Data Structure
Start with the question the school needs answered
A school needs a presentation order, a unique attendance count, a mark lookup by ID, an undo history and a help-desk waiting line. One container can sometimes model several tasks, but the access rules differ.
A data structure organizes values for particular operations. Choose based on the required behavior first, then evaluate costs for the actual workload.
Map requirements
Identify order, uniqueness, key lookup or removal policy.
Compare trade-offs
Consider size, mutation patterns and memory after establishing correctness.
Justify a choice
Explain why a reasonable alternative would behave differently.
Different questions need different access rules
Presentation order
Who is second?
ArrayUnique attendance
Has this ID already attended?
SetMarks lookup
Which mark belongs to ID 007?
Map
Write the operations before selecting a container
An array fits ordered sequences with positional reads. A set fits distinct membership. A map fits key-value association. A stack removes recent items first; a queue removes earlier arrivals first. A linked list supports link-based traversal and updates at known locations.
These are not rankings. Consider repeated values, stable keys, update frequency, required order, typical size and memory. Also distinguish local data structure behavior from persistence or distributed coordination.
Before starting, review the structures introduced in this group. These examples use JavaScript; other languages can use different syntax and rules.
Compare three school requirements directly
The presentation array retrieves the second name. The attendance set collapses a duplicate identifier. The marks map retrieves a value by a stable key.
Choosing a set for presentations would discard repeated occurrences that might be meaningful. Choosing a raw array for repeated ID lookups can require searching entries.
JavaScript · local teaching example
const presentationOrder = ['Maya', 'Ravi'];
const attended = new Set(['007', '007', '009']);
const marksById = new Map([['007', 40], ['009', 28]]);
console.log(presentationOrder[1]);
console.log(attended.size);
console.log(marksById.get('007'));Expected output
Ravi
2
40The same arrivals can produce different removal orders
A stack removes Ravi, the newest entry. A queue removes Maya, the earliest entry. The stored names are identical; the rule changes the result.
Select the rule appropriate to undo or a waiting line. A name like tasks does not enforce either contract.
JavaScript · local teaching example
const stack = ['Maya', 'Ravi'];
const queue = ['Maya', 'Ravi'];
console.log(stack.pop());
console.log(queue.shift());Expected output
Ravi
MayaApply the rule to a small change
A school must preserve all attendance scans including duplicates, while also reporting unique IDs. Keep both views and display their counts.
Worked answer
An array keeps the three scan events and a derived set reports two unique identifiers. Different views can coexist; the event log should not be replaced merely because the report needs deduplication.
JavaScript · local teaching example
const scans = ['007', '009', '007'];
const uniqueIds = new Set(scans);
console.log(scans.length);
console.log(uniqueIds.size);Expected output
3
2Recognize mistakes and limits
Do not assume exact operation costs solely from a container name across languages. Implementations differ, and worst-case or average-case behavior depends on details. Measure a representative workload when performance matters.
Keeping multiple views can improve reads but creates update-consistency work. Define the authoritative data and how derived collections are rebuilt. Persistent storage, crash recovery and concurrent updates need additional design.
For the language or tool rules used here, see JavaScript indexed collections.
Check the core idea
Apply the idea to a new situation
Explain the behavior your choice provides
Choose by operation and identity requirements. Separate order, uniqueness, lookup and removal policy, then compare memory and update costs for a realistic workload.
Connect this idea with collections and records and responsibilities around state.
Reaching the recap and completing the quizzes are separate milestones.