What is the average time complexity for lookup in a Hash Map?
The average time complexity for lookup in a Hash Map is O(1), meaning it can find an element very quickly using a key.
Click to reveal answer
beginner
How does lookup work in an Array?
Lookup in an Array is O(1) if you know the index because you can directly access the element. But if you search by value, it is O(n) because you may need to check each element.
Click to reveal answer
beginner
Why is lookup in a Linked List slower than in an Array or Hash Map?
Lookup in a Linked List is O(n) because you must start at the head and check each node one by one until you find the target.
Click to reveal answer
intermediate
What is a key advantage of using a Hash Map over an Array or Linked List for lookup?
A Hash Map provides fast lookup by key with average O(1) time, which is much faster than searching through an Array or Linked List when you don't know the index.
Click to reveal answer
intermediate
When might you prefer using an Array over a Hash Map or Linked List for lookup?
You might prefer an Array when you need fast access by index and the data size is fixed or small, because Arrays use less memory and have simple structure.
Click to reveal answer
What is the average time complexity of lookup in a Hash Map?
AO(1)
BO(n)
CO(log n)
DO(n log n)
✗ Incorrect
Hash Maps use keys to directly access values, making average lookup time O(1).
If you want to find a value in an unsorted Array by searching each element, what is the time complexity?
AO(n)
BO(log n)
CO(1)
DO(n^2)
✗ Incorrect
You may need to check each element, so the time is O(n).
Which data structure requires checking nodes one by one for lookup?
AHash Map
BArray
CBinary Search Tree
DLinked List
✗ Incorrect
Linked Lists store elements in nodes connected by pointers, so you check nodes one by one.
Which data structure provides fastest lookup by index?
AHash Map
BLinked List
CArray
DStack
✗ Incorrect
Arrays allow direct access by index in O(1) time.
When is a Hash Map less efficient than an Array for lookup?
AWhen data is unsorted
BWhen you know the exact index
CWhen data is large
DWhen keys are strings
✗ Incorrect
If you know the index, Array lookup is faster and simpler than Hash Map.
Explain the differences in lookup time complexity between Hash Map, Array, and Linked List.
Think about how each data structure accesses elements.
You got /3 concepts.
Describe scenarios where you would choose an Array, Linked List, or Hash Map for lookup operations.
Consider speed and type of access needed.
You got /3 concepts.
Practice
(1/5)
1. Which data structure provides the fastest average lookup time for finding an element by key?
easy
A. Stack
B. Hash Map
C. Linked List
D. Array
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 B
Quick Check:
Fastest lookup by key = Hash Map [OK]
Hint: Hash Map lookup is usually O(1), faster than arrays or lists [OK]
Common Mistakes:
Confusing array index access with key lookup
Thinking linked list lookup is fast
Assuming stack is good for key lookup
2. Which of the following is the correct way to declare an array of 5 integers in C?
easy
A. int arr(5);
B. int arr = new int[5];
C. array<int> arr(5);
D. int arr[5];
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 D
Quick Check:
C array declaration = int arr[5]; [OK]
Hint: C arrays use square brackets for size, no new keyword [OK]
Common Mistakes:
Using C++ new keyword in C
Using parentheses instead of brackets
Confusing C++ templates with C arrays
3. Consider the following C code snippet using a linked list for lookup:
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);?
medium
A. 20
B. 10
C. 30
D. -1
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 A
Quick Check:
Found key 2 with value 20 [OK]
Hint: Linked list lookup scans nodes until key found [OK]
Common Mistakes:
Returning value of first node always
Confusing key and value fields
Returning -1 when key exists
4. The following C code attempts to find a value in an array but has a bug:
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?
medium
A. Return value should be arr[i], not i
B. Function should return void
C. Loop condition should be i < size, not i <= size
D. Array parameter should be int* arr
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 C
Quick Check:
Array index must be less than size [OK]
Hint: Array indices go 0 to size-1, use i < size in loops [OK]
Common Mistakes:
Using <= size causes out-of-bounds access
Returning arr[i] instead of index
Changing return type unnecessarily
5. You need to store a large number of unique integer keys with associated values and perform frequent lookups. Which data structure is best and why?
hard
A. Hash Map - because it provides average constant time lookup
B. Linked List - because it allows easy insertion
C. Array - because it uses contiguous memory
D. Binary Search Tree - because it keeps keys sorted
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 A
Quick Check:
Fast lookup with many keys = Hash Map [OK]
Hint: For fast key lookup, choose Hash Map over arrays or lists [OK]
Common Mistakes:
Choosing array for non-index keys
Ignoring lookup speed in linked lists
Preferring BST without sorting need
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.