Challenge - 5 Problems
Lookup Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateLookup Time Complexity in Different Data Structures
Consider the following JavaScript code snippets that perform lookup operations. Which option correctly shows the output time complexity for each data structure?
DSA Javascript
const array = [1, 2, 3, 4, 5]; const linkedList = { head: { value: 1, next: { value: 2, next: null } } }; const hashMap = new Map([[1, 'a'], [2, 'b'], [3, 'c']]);
Attempts:
2 left
💡 Hint
Think about how each data structure stores and accesses elements.
✗ Incorrect
Arrays and linked lists require scanning elements for lookup, so they take O(n) time. Hash maps use keys to directly access values, so lookup is O(1).
🧠 Conceptual
intermediateBest Data Structure for Fast Lookup by Key
Which data structure is best suited for fast lookup by a unique key in JavaScript?
Attempts:
2 left
💡 Hint
Consider which structure uses keys to access values directly.
✗ Incorrect
Hash maps store key-value pairs and allow direct access by key, making lookup very fast.
❓ Predict Output
advancedOutput of Lookup in Linked List vs Array
What will be the output of the following JavaScript code?
DSA Javascript
const array = [10, 20, 30, 40]; const linkedList = { value: 10, next: { value: 20, next: { value: 30, next: null } } }; function lookupArray(arr, target) { return arr.includes(target); } function lookupLinkedList(node, target) { while (node !== null) { if (node.value === target) return true; node = node.next; } return false; } console.log(lookupArray(array, 30)); console.log(lookupLinkedList(linkedList, 40));
Attempts:
2 left
💡 Hint
Check if the target exists in each data structure.
✗ Incorrect
The array contains 30, so lookupArray returns true. The linked list does not contain 40, so lookupLinkedList returns false.
🔧 Debug
advancedIdentify the Error in Hash Map Lookup Code
What error will the following JavaScript code produce when trying to lookup a key in a hash map?
DSA Javascript
const hashMap = new Map([[1, 'one'], [2, 'two']]); function getValue(key) { return hashMap[key]; } console.log(getValue(1));
Attempts:
2 left
💡 Hint
How do you access values in a Map object?
✗ Incorrect
Using bracket notation on a Map returns undefined because Map requires the get() method for lookup.
🧠 Conceptual
expertWhy Linked List Lookup is Slower than Hash Map Lookup
Why is lookup in a linked list generally slower than lookup in a hash map?
Attempts:
2 left
💡 Hint
Think about how each data structure finds an element.
✗ Incorrect
Linked lists require checking each node one by one, which takes longer. Hash maps use a hash function to jump directly to the element.
