Hash Map vs Array vs Linked List for Lookup in DSA C - Complexity Comparison
We want to understand how fast we can find a value in different data structures.
The question is: How does the time to find something change as the data grows?
Analyze the time complexity of the following lookup operations.
// Lookup in array
int lookup_array(int arr[], int n, int target) {
for (int i = 0; i < n; i++) {
if (arr[i] == target) return i;
}
return -1;
}
// Lookup in linked list
int lookup_linked_list(Node* head, int target) {
Node* current = head;
while (current != NULL) {
if (current->value == target) return 1;
current = current->next;
}
return 0;
}
// Lookup in hash map (simplified)
int lookup_hash_map(HashMap* map, int target) {
int index = hash(target) % map->capacity;
Entry* entry = map->buckets[index];
while (entry != NULL) {
if (entry->key == target) return 1;
entry = entry->next;
}
return 0;
}
This code shows how to find a value in an array, a linked list, and a hash map.
Look at what repeats when searching:
- Array: Loop through each element one by one.
- Linked List: Traverse nodes one by one until found or end.
- Hash Map: Check entries in one bucket's linked list after hashing.
- Dominant operation: Number of elements checked in the search.
As the number of items (n) grows, how many checks happen?
| Input Size (n) | Array & Linked List Checks | Hash Map Checks (average) |
|---|---|---|
| 10 | Up to 10 checks | About 1 check |
| 100 | Up to 100 checks | About 1 check |
| 1000 | Up to 1000 checks | About 1 check |
Array and linked list checks grow directly with n. Hash map checks stay roughly the same if well sized.
Time Complexity: O(n) for array and linked list, O(1) average for hash map lookup
This means array and linked list take longer as data grows, but hash map stays fast on average.
[X] Wrong: "Hash map lookup is always O(1) no matter what."
[OK] Correct: If many keys collide, hash map lookup can slow down to O(n) in worst case.
Knowing these differences helps you choose the right data structure and explain your choices clearly.
"What if the hash map uses a poor hash function causing many collisions? How would the lookup time change?"
Practice
Solution
Step 1: Understand lookup times for each data structure
Hash Map uses hashing to find keys quickly, usually in constant time O(1). Arrays require scanning or index access, O(1) if index known but O(n) if searching by value. Linked Lists require scanning all nodes, O(n).Step 2: Compare average lookup times
Hash Map is fastest on average for key lookup due to direct hashing. Arrays and Linked Lists are slower for key-based search.Final Answer:
Hash Map -> Option BQuick Check:
Fastest lookup by key = Hash Map [OK]
- Confusing array index access with key lookup
- Thinking linked list lookup is fast
- Assuming stack is good for key lookup
Solution
Step 1: Recall C array declaration syntax
In C, arrays are declared with type, name, and size in square brackets, e.g., int arr[5];Step 2: Check each option
int arr[5]; matches correct C syntax. int arr = new int[5]; uses C++ style new keyword. int arr(5); is invalid syntax. array<int> arr(5); is C++ template syntax, not C.Final Answer:
int arr[5]; -> Option DQuick Check:
C array declaration = int arr[5]; [OK]
- Using C++ new keyword in C
- Using parentheses instead of brackets
- Confusing C++ templates with C arrays
typedef struct Node {
int key;
int value;
struct Node* next;
} Node;
int lookup(Node* head, int key) {
Node* current = head;
while (current != NULL) {
if (current->key == key) {
return current->value;
}
current = current->next;
}
return -1;
}
// List: (1,10) -> (2,20) -> (3,30) -> NULL
// Call: lookup(head, 2);
What is the output of lookup(head, 2);?Solution
Step 1: Trace the linked list nodes
The list nodes have keys and values: (1,10), (2,20), (3,30). The function searches for key 2.Step 2: Follow lookup function logic
Starting at head (key=1), key != 2, move next. Next node key=2 matches, return value 20.Final Answer:
20 -> Option AQuick Check:
Found key 2 with value 20 [OK]
- Returning value of first node always
- Confusing key and value fields
- Returning -1 when key exists
int find(int arr[], int size, int target) {
for (int i = 0; i <= size; i++) {
if (arr[i] == target) {
return i;
}
}
return -1;
}
What is the bug in this code?Solution
Step 1: Analyze loop boundary
The loop runs from i = 0 to i <= size, which means i can be equal to size. Array indices go from 0 to size-1, so arr[size] is out of bounds.Step 2: Correct loop condition
Change condition to i < size to avoid accessing out of bounds memory.Final Answer:
Loop condition should be i < size, not i <= size -> Option CQuick Check:
Array index must be less than size [OK]
- Using <= size causes out-of-bounds access
- Returning arr[i] instead of index
- Changing return type unnecessarily
Solution
Step 1: Understand requirements
We need to store many unique keys with values and do frequent lookups efficiently.Step 2: Evaluate data structures
Array is inefficient for key lookup unless keys are indices. Linked List has slow O(n) lookup. Binary Search Tree has O(log n) lookup but more complex. Hash Map offers average O(1) lookup, ideal for fast access.Final Answer:
Hash Map - because it provides average constant time lookup -> Option AQuick Check:
Fast lookup with many keys = Hash Map [OK]
- Choosing array for non-index keys
- Ignoring lookup speed in linked lists
- Preferring BST without sorting need
Ready to prove it?
Pass the test and this pattern moves unknown
