📖 Scenario: You have a list of numbers that was originally sorted but then rotated at some unknown point. For example, a sorted list like [1, 2, 3, 4, 5] might become [4, 5, 1, 2, 3]. You want to find if a specific number exists in this rotated list.
🎯 Goal: Build a Rust program that searches for a target number in a rotated sorted array and returns its index if found, or -1 if not found.
📋 What You'll Learn
Create a vector called nums with the exact values [4, 5, 6, 7, 0, 1, 2]
Create a variable called target and set it to 0
Implement a function called search_rotated that takes nums and target as parameters and returns the index of target in nums or -1 if not found
Use binary search logic adapted for rotated sorted arrays inside search_rotated
Print the result of calling search_rotated(&nums, target)
💡 Why This Matters
🌍 Real World
Rotated sorted arrays appear in systems where data is shifted or rotated, such as circular buffers or rotated logs. Efficient searching in such arrays is important for fast data retrieval.
💼 Career
Understanding how to search in rotated sorted arrays is a common interview question and helps develop problem-solving skills for software engineering roles.
Progress0 / 4 steps
1
Create the rotated sorted array
Create a vector called nums with the exact values [4, 5, 6, 7, 0, 1, 2]
DSA Rust
Hint
Use let nums = vec![4, 5, 6, 7, 0, 1, 2]; to create the vector.
2
Set the target value to search
Create a variable called target and set it to 0
DSA Rust
Hint
Use let target = 0; to create the target variable.
3
Implement the search function
Implement a function called search_rotated that takes nums: &Vec and target: i32 as parameters and returns an i32. Use binary search logic adapted for rotated sorted arrays inside search_rotated.
DSA Rust
Hint
Use a while loop with left and right pointers. Compare nums[mid] with target and adjust pointers based on rotated sorted array logic.
4
Print the search result
Print the result of calling search_rotated(&nums, target) inside the main function.
DSA Rust
Hint
Use println!("{}", index); to print the index returned by search_rotated.
Practice
(1/5)
1. What is the main challenge when searching for a target in a rotated sorted array compared to a normal sorted array?
easy
A. The array size is unknown
B. The array contains duplicate elements
C. The array is sorted in descending order
D. The array is split into two sorted parts, making direct binary search tricky
Solution
Step 1: Understand the array structure
A rotated sorted array is originally sorted but then rotated at some pivot, splitting it into two sorted parts.
Step 2: Impact on search
This split means a normal binary search can't be applied directly because the order is disrupted at the pivot.
Final Answer:
The array is split into two sorted parts, making direct binary search tricky -> Option D
Quick Check:
Rotated array = two sorted parts [OK]
Hint: Look for the pivot splitting the array into two sorted halves [OK]
Common Mistakes:
Assuming the array is fully sorted without rotation
Ignoring the pivot point in the array
Thinking duplicates cause the main challenge
Confusing descending order with rotation
2. Which Rust code snippet correctly declares a function signature to search a target in a rotated sorted array?
easy
A. fn search(nums: &[i32], target: i32) -> Option<usize>
B. fn search(nums: Vec<i32>, target: i32) -> usize
C. fn search(nums: &Vec<i32>, target: i32) -> bool
D. fn search(nums: &[i32], target: i32) -> i32
Solution
Step 1: Analyze parameter types
Using a slice &[i32] is efficient and idiomatic for read-only array input in Rust.
Step 2: Analyze return type
Returning Option<usize> allows indicating if the target was found (Some(index)) or not (None).
Final Answer:
fn search(nums: &[i32], target: i32) -> Option<usize> -> Option A