Challenge - 5 Problems
Linked List Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of searching for value 3 in the linked list?
Consider the linked list created by inserting values 1, 2, 3, 4 in order. What will be the output of searching for the value 3?
DSA Rust
struct Node {
value: i32,
next: Option<Box<Node>>,
}
struct LinkedList {
head: Option<Box<Node>>,
}
impl LinkedList {
fn new() -> Self {
LinkedList { head: None }
}
fn append(&mut self, val: i32) {
let new_node = Box::new(Node { value: val, next: None });
match self.head.as_mut() {
None => self.head = Some(new_node),
Some(mut node) => {
while let Some(ref mut next_node) = node.next {
node = next_node;
}
node.next = Some(new_node);
}
}
}
fn search(&self, val: i32) -> bool {
let mut current = &self.head;
while let Some(node) = current {
if node.value == val {
return true;
}
current = &node.next;
}
false
}
}
fn main() {
let mut list = LinkedList::new();
list.append(1);
list.append(2);
list.append(3);
list.append(4);
println!("{}", list.search(3));
}Attempts:
2 left
💡 Hint
Think about whether the value 3 exists in the linked list nodes.
✗ Incorrect
The search function traverses the linked list and returns true if it finds the value 3. Since 3 was appended, the output is true.
❓ Predict Output
intermediateWhat is the output when searching for a value not in the list?
Given the linked list with values 5, 10, 15, what is the output of searching for the value 7?
DSA Rust
struct Node {
value: i32,
next: Option<Box<Node>>,
}
struct LinkedList {
head: Option<Box<Node>>,
}
impl LinkedList {
fn new() -> Self {
LinkedList { head: None }
}
fn append(&mut self, val: i32) {
let new_node = Box::new(Node { value: val, next: None });
match self.head.as_mut() {
None => self.head = Some(new_node),
Some(mut node) => {
while let Some(ref mut next_node) = node.next {
node = next_node;
}
node.next = Some(new_node);
}
}
}
fn search(&self, val: i32) -> bool {
let mut current = &self.head;
while let Some(node) = current {
if node.value == val {
return true;
}
current = &node.next;
}
false
}
}
fn main() {
let mut list = LinkedList::new();
list.append(5);
list.append(10);
list.append(15);
println!("{}", list.search(7));
}Attempts:
2 left
💡 Hint
Check if the value 7 is in the list nodes.
✗ Incorrect
The search function returns false because 7 is not found in the linked list.
🔧 Debug
advancedWhat error does this code produce when searching in an empty list?
Analyze the code below. What error will occur when searching for any value in an empty linked list?
DSA Rust
struct Node {
value: i32,
next: Option<Box<Node>>,
}
struct LinkedList {
head: Option<Box<Node>>,
}
impl LinkedList {
fn new() -> Self {
LinkedList { head: None }
}
fn search(&self, val: i32) -> bool {
let mut current = &self.head;
while let Some(node) = current {
if node.value == val {
return true;
}
current = &node.next;
}
false
}
}
fn main() {
let list = LinkedList::new();
println!("{}", list.search(1));
}Attempts:
2 left
💡 Hint
Consider how the search function handles None head.
✗ Incorrect
The search function safely handles an empty list by checking if head is None and returns false without error.
🧠 Conceptual
advancedHow many nodes are traversed when searching for value 4 in the list 1->2->3->4->5?
Given a linked list with nodes containing values 1, 2, 3, 4, 5 in order, how many nodes does the search function visit when searching for the value 4?
Attempts:
2 left
💡 Hint
Search visits nodes until it finds the value or reaches the end.
✗ Incorrect
The search visits nodes with values 1, 2, 3, and 4 before finding the target, so 4 nodes are visited.
🚀 Application
expertWhat is the output after searching for value 10 in a reversed linked list?
A linked list is created by appending 5, 10, 15, then reversed to 15->10->5. What is the output of searching for value 10 after reversal?
DSA Rust
struct Node {
value: i32,
next: Option<Box<Node>>,
}
struct LinkedList {
head: Option<Box<Node>>,
}
impl LinkedList {
fn new() -> Self {
LinkedList { head: None }
}
fn append(&mut self, val: i32) {
let new_node = Box::new(Node { value: val, next: None });
match self.head.as_mut() {
None => self.head = Some(new_node),
Some(mut node) => {
while let Some(ref mut next_node) = node.next {
node = next_node;
}
node.next = Some(new_node);
}
}
}
fn reverse(&mut self) {
let mut prev = None;
let mut current = self.head.take();
while let Some(mut boxed_node) = current {
let next = boxed_node.next.take();
boxed_node.next = prev;
prev = Some(boxed_node);
current = next;
}
self.head = prev;
}
fn search(&self, val: i32) -> bool {
let mut current = &self.head;
while let Some(node) = current {
if node.value == val {
return true;
}
current = &node.next;
}
false
}
}
fn main() {
let mut list = LinkedList::new();
list.append(5);
list.append(10);
list.append(15);
list.reverse();
println!("{}", list.search(10));
}Attempts:
2 left
💡 Hint
Reversing the list does not remove any nodes, so searching for 10 should still succeed.
✗ Incorrect
The reverse function reverses the list but keeps all nodes. Searching for 10 returns true.
