Challenge - 5 Problems
Master of Lookup Structures
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateLookup time comparison: Array vs Linked List
What is the output of the following Go code that searches for the value 5 in an array and a linked list?
DSA Go
package main import "fmt" type Node struct { val int next *Node } func searchArray(arr []int, target int) bool { for _, v := range arr { if v == target { return true } } return false } func searchLinkedList(head *Node, target int) bool { current := head for current != nil { if current.val == target { return true } current = current.next } return false } func main() { arr := []int{1, 3, 5, 7, 9} head := &Node{1, &Node{3, &Node{5, &Node{7, &Node{9, nil}}}}} fmt.Println(searchArray(arr, 5)) fmt.Println(searchLinkedList(head, 5)) }
Attempts:
2 left
💡 Hint
Both array and linked list contain the value 5, so searching should find it.
✗ Incorrect
The searchArray function iterates over the array and finds 5, returning true. The searchLinkedList function traverses the linked list nodes and finds 5, returning true as well.
❓ Predict Output
intermediateHash Map lookup result
What is the output of this Go code that looks up a key in a hash map?
DSA Go
package main import "fmt" func main() { m := map[string]int{"apple": 5, "banana": 3} val, ok := m["banana"] fmt.Println(val, ok) }
Attempts:
2 left
💡 Hint
The key "banana" exists in the map with value 3.
✗ Incorrect
The map contains "banana" with value 3, so val is 3 and ok is true indicating the key exists.
🧠 Conceptual
advancedTime complexity of lookup in data structures
Which data structure provides the fastest average lookup time for searching an element by key?
Attempts:
2 left
💡 Hint
Think about how many elements you check on average in each structure.
✗ Incorrect
Hash maps provide average O(1) lookup time by using a hash function to directly access the bucket. Arrays and linked lists require checking elements one by one (O(n)). Binary trees can provide O(log n) if balanced, but linear search is O(n).
🔧 Debug
advancedWhy does this linked list lookup fail?
Given this Go code to search a linked list, what is the reason it always returns false?
DSA Go
package main import "fmt" type Node struct { val int next *Node } func search(head *Node, target int) bool { current := head for current.next != nil { if current.val == target { return true } current = current.next } return false } func main() { head := &Node{1, &Node{2, &Node{3, nil}}} fmt.Println(search(head, 3)) }
Attempts:
2 left
💡 Hint
Check the loop condition carefully and what nodes it visits.
✗ Incorrect
The loop condition 'current.next != nil' stops before the last node, so the last node's value is never checked. This causes the function to miss the target if it is in the last node.
🚀 Application
expertChoosing the best data structure for frequent lookups and insertions
You need a data structure to store user IDs and quickly check if a user exists. You also need to add new users frequently. Which data structure is best?
Attempts:
2 left
💡 Hint
Consider both lookup and insertion speed for large data.
✗ Incorrect
Hash maps provide average O(1) time for both lookup and insertion, making them ideal for frequent existence checks and additions. Arrays and linked lists have slower lookups (O(n)). Binary search trees have O(log n) but are more complex.
