What if you could check every item in a list perfectly without missing a single one?
Why Array Traversal Patterns in DSA Rust?
Imagine you have a long list of your favorite songs written on paper. You want to find all songs by a certain artist. You start reading each song one by one from the top to the bottom.
Doing this by hand is slow and tiring. You might lose your place, skip songs, or read the same song twice. It's easy to make mistakes and waste time.
Array traversal patterns are like having a clear, step-by-step way to look at each song in your list without missing or repeating any. This helps computers quickly and correctly check every item in a list.
let songs = ["Song1", "Song2", "Song3"]; // Manually check each song one by one if songs[0] == "ArtistA" { /* do something */ } if songs[1] == "ArtistA" { /* do something */ } if songs[2] == "ArtistA" { /* do something */ }
let songs = ["ArtistA", "Song2", "Song3"]; for song in songs.iter() { if song == &"ArtistA" { // do something } }
It lets us easily and safely look at every item in a list to find, change, or count things without mistakes.
When you scroll through your phone's photo gallery, the app uses array traversal to show each picture one after another smoothly.
Manual checking is slow and error-prone.
Traversal patterns give a clear way to visit each item.
This makes working with lists fast, safe, and easy.
Practice
for i in 0..5 { println!("{}", i); }Solution
Step 1: Understand the range syntax 0..5
The range 0..5 in Rust includes numbers starting at 0 up to but not including 5.Step 2: Trace the loop iterations
The loop variable i takes values 0, 1, 2, 3, and 4, printing each number.Final Answer:
Prints numbers from 0 to 4 -> Option DQuick Check:
Range 0..5 excludes 5, so output is 0 to 4 [OK]
- Thinking 0..5 includes 5
- Confusing 0..5 with 0..=5
- Assuming loop starts at 1
arr in Rust?Solution
Step 1: Check valid iteration over array indices
Rust arrays have a length method .len(), so 0..arr.len() gives valid indices.Step 2: Access elements by index
Using arr[i] inside the loop accesses each element correctly.Final Answer:
for i in 0..arr.len() { println!("{}", arr[i]); } -> Option AQuick Check:
Use 0..arr.len() for index-based iteration [OK]
- Using 0..arr instead of 0..arr.len()
- Trying to iterate directly over arr without reference
- Using arr.len() without range syntax
let arr = [10, 20, 30];
for i in (0..arr.len()).rev() {
print!("{} ", arr[i]);
}Solution
Step 1: Understand the range and reverse
The range 0..arr.len() is 0..3, which is 0,1,2. Using .rev() reverses it to 2,1,0.Step 2: Print elements in reverse order
arr[2] = 30, arr[1] = 20, arr[0] = 10, so output is "30 20 10 "Final Answer:
30 20 10 -> Option CQuick Check:
Reverse iteration prints elements backward [OK]
- Forgetting .rev() reverses the order
- Printing indices instead of array values
- Confusing range end with inclusive
arr:let arr = [1, 2, 3];
for i in 0..arr.len() {
println!("{}", arr[i+1]);
}Solution
Step 1: Analyze the loop indices and access
The loop runs i from 0 to 2 (arr.len()=3). Accessing arr[i+1] means indices 1,2,3.Step 2: Identify out of bounds access
arr[3] does not exist, causing runtime panic (index out of bounds).Final Answer:
Index out of bounds error at runtime -> Option BQuick Check:
Accessing arr[i+1] exceeds array length [OK]
- Assuming arr[i+1] is safe for all i
- Ignoring runtime panics
- Confusing compile-time and runtime errors
arr of integers, which code snippet correctly prints only the even-indexed elements (0, 2, 4, ...) of the array?Solution
Step 1: Understand step_by usage
The method step_by(2) skips every second index, so 0, 2, 4, ...Step 2: Confirm correct element access
Using arr[i] inside the loop prints elements at even indices correctly.Final Answer:
for i in (0..arr.len()).step_by(2) { println!("{}", arr[i]); } -> Option AQuick Check:
step_by(2) iterates over even indices [OK]
- Using i % 2 == 1 instead of 0 for even indices
- Multiplying i by 2 causing out of bounds
- Using rev() which reverses order unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
