Which data structure is best for storing a collection of key-value pairs?
Trap 1: Queue
Queues enforce first-in-first-out ordering, exposing only enqueue and dequeue operations, so no key can address a specific value. They are tempting because they manage ordered processing, and would be correct for scheduling or buffering tasks rather than key-value storage.
Trap 2: Array
Arrays index elements by sequential integer position, so they cannot associate a lookup key with a stored value. They are tempting because they store ordered collections efficiently, and would be correct when sequence and positional access matter rather than key-based retrieval.
Trap 3: List
Lists hold ordered elements accessed by numeric index, not by arbitrary key, so they cannot map keys to values. They are tempting because they store heterogeneous collections flexibly, and would be correct when order and iteration matter rather than keyed lookup.
- A
Queue
Why it fails: Queues enforce first-in-first-out ordering, exposing only enqueue and dequeue operations, so no key can address a specific value. They are tempting because they manage ordered processing, and would be correct for scheduling or buffering tasks rather than key-value storage.
- B
Array
Why it fails: Arrays index elements by sequential integer position, so they cannot associate a lookup key with a stored value. They are tempting because they store ordered collections efficiently, and would be correct when sequence and positional access matter rather than key-based retrieval.
- C
Dictionary
A dictionary stores data as key-value pairs, mapping each unique key directly to its associated value for fast lookup by key. Lists, tuples and sets hold ordered or unique elements without key association, so they cannot satisfy the stem's key-value pairing requirement.
- D
List
Why it fails: Lists hold ordered elements accessed by numeric index, not by arbitrary key, so they cannot map keys to values. They are tempting because they store heterogeneous collections flexibly, and would be correct when order and iteration matter rather than keyed lookup.