Consider these three data structures holding 1 million integers: a hash map, an array, and a linked list. Which one will generally provide the fastest lookup for a specific value?
Think about how each data structure finds an element and the time it takes.
Hash maps use hashing to jump directly to the element's location, giving near constant time lookup. Arrays require searching (linear or binary if sorted), and linked lists require sequential traversal, which is slower.
What will be the output of this Rust code simulating lookup in different data structures?
use std::collections::HashMap;
fn main() {
let mut map = HashMap::new();
let array = [10, 20, 30, 40, 50];
let linked_list = vec![10, 20, 30, 40, 50]; // simulate linked list with Vec
map.insert(1, 10);
map.insert(2, 20);
map.insert(3, 30);
map.insert(4, 40);
map.insert(5, 50);
let key = 3;
let map_lookup = map.get(&key);
let array_lookup = array.iter().find(|&&x| x == 30);
let list_lookup = linked_list.iter().find(|&&x| x == 30);
println!("Map lookup: {:?}", map_lookup);
println!("Array lookup: {:?}", array_lookup);
println!("Linked list lookup: {:?}", list_lookup);
}Check how the key and values are matched in each lookup.
The map lookup uses key 3 which maps to value 30, so Some(30). The array and linked list searches for value 30, which exists, so both return Some(30).
Why does a linked list generally have slower lookup times compared to an array?
Think about how memory layout affects access speed.
Linked lists store elements scattered in memory, so to find an element you must start at the head and follow links one by one, which is slower than direct index access in arrays.
What is the output of this Rust code that measures lookup times in a vector and a hash map?
use std::collections::HashMap;
use std::time::Instant;
fn main() {
let size = 1000000;
let mut vec = Vec::new();
let mut map = HashMap::new();
for i in 0..size {
vec.push(i);
map.insert(i, i);
}
let target = size - 1;
let start_vec = Instant::now();
let found_vec = vec.iter().find(|&&x| x == target);
let duration_vec = start_vec.elapsed();
let start_map = Instant::now();
let found_map = map.get(&target);
let duration_map = start_map.elapsed();
println!("Vector lookup found: {:?} in {:?}", found_vec, duration_vec);
println!("HashMap lookup found: {:?} in {:?}", found_map, duration_map);
}Consider the time complexity of searching in a vector vs a hash map.
Vector lookup uses linear search, which takes longer especially near the end. Hash map lookup uses hashing for near constant time, so it is faster.
You need a data structure for a program that does frequent lookups and insertions of unique keys. Which data structure is best and why?
Think about average time complexity for lookup and insertion in each data structure.
Hash maps provide average O(1) time for both lookup and insertion. Arrays have slow insertions except at the end and slow lookups unless sorted. Linked lists have slow lookups. Binary trees have O(log n) time, not constant.
