
Practice 50 Vector Coding Questions
Q1. Write a Rust program to create a vector with five integers and print its elements.
Input:
Vector: [1, 2, 3, 4, 5]
Expected Output:
1 2 3 4 5
Q2. Write a Rust program to add an element to a vector and print the vector.
Input:
Initial vector: [1, 2, 3], Element to add: 4
Expected Output:
[1, 2, 3, 4]
Q3. Write a Rust program to remove an element from a vector and print the resulting vector.
Input:
Initial vector: [1, 2, 3, 4], Element to remove: 3
Expected Output:
[1, 2, 4]
Q4. Write a Rust program to find the length of a vector.
Input:
Vector: [1, 2, 3, 4]
Expected Output:
Length: 4
Q5. Write a Rust program to check if a vector is empty.
Input:
Vector: []
Expected Output:
The vector is empty.
Q6. Write a Rust program to create a vector of strings and print all the elements.
Input:
Vector: ["apple", "banana", "cherry"]
Expected Output:
apple banana cherry
Q7. Write a Rust program to iterate over a vector and print each element.
Input:
Vector: [10, 20, 30, 40]
Expected Output:
10 20 30 40
Q8. Write a Rust program to check if a specific element exists in a vector.
Input:
Vector: [1, 2, 3, 4], Element: 3
Expected Output:
Element found in the vector.
Q9. Write a Rust program to reverse the elements of a vector.
Input:
Vector: [1, 2, 3, 4]
Expected Output:
[4, 3, 2, 1]
Q10. Write a Rust program to find the maximum element in a vector of integers.
Input:
Vector: [10, 20, 5, 30]
Expected Output:
Maximum element: 30
Q11. Write a Rust program to sum all the elements of a vector.
Input:
Vector: [1, 2, 3, 4]
Expected Output:
Sum: 10
Q12. Write a Rust program to remove all even numbers from a vector.
Input:
Vector: [1, 2, 3, 4, 5]
Expected Output:
[1, 3, 5]
Q13. Write a Rust program to sort a vector in ascending order.
Input:
Vector: [3, 1, 4, 2]
Expected Output:
[1, 2, 3, 4]
Q14. Write a Rust program to find the index of an element in a vector.
Input:
Vector: [10, 20, 30, 40], Element: 30
Expected Output:
Index of 30: 2
Q15. Write a Rust program to remove all duplicate elements from a vector.
Input:
Vector: [1, 2, 2, 3, 4, 4]
Expected Output:
[1, 2, 3, 4]
Q16. Write a Rust program to create a vector from a range of integers.
Input:
Range: 1..6
Expected Output:
[1, 2, 3, 4, 5]
Q17. Write a Rust program to create a vector of vectors and access an inner vector.
Input:
Vector: [[1, 2], [3, 4], [5, 6]]
Expected Output:
Inner vector: [3, 4]
Q18. Write a Rust program to append one vector to another.
Input:
Vector 1: [1, 2, 3], Vector 2: [4, 5]
Expected Output:
[1, 2, 3, 4, 5]
Q19. Write a Rust program to double each element in a vector.
Input:
Vector: [1, 2, 3]
Expected Output:
[2, 4, 6]
Q20. Write a Rust program to create a vector of squares of integers from 1 to 5.
Input:
Range: 1..6
Expected Output:
[1, 4, 9, 16, 25]
Q21. Write a Rust program to create a vector and access an element by index.
Input:
Vector: [10, 20, 30], Index: 1
Expected Output:
Element at index 1: 20
Q22. Write a Rust program to convert a vector to a string.
Input:
Vector: ["hello", "world"]
Expected Output:
hello world
Q23. Write a Rust program to check if a vector contains only positive integers.
Input:
Vector: [1, 2, 3, 4]
Expected Output:
The vector contains only positive integers.
Q24. Write a Rust program to create a vector of floating point numbers and calculate their average.
Input:
Vector: [1.5, 2.5, 3.5]
Expected Output:
Average: 2.5
Q25. Write a Rust program to check if a vector is sorted in ascending order.
Input:
Vector: [1, 2, 3, 4]
Expected Output:
The vector is sorted in ascending order.
Q26. Write a Rust program to find the smallest element in a vector.
Input:
Vector: [10, 20, 5, 30]
Expected Output:
Smallest element: 5
Q27. Write a Rust program to create a vector of characters and convert it to a string.
Input:
Vector: ['H', 'e', 'l', 'l', 'o']
Expected Output:
Hello
Q28. Write a Rust program to count the occurrences of an element in a vector.
Input:
Vector: [1, 2, 3, 1, 1], Element: 1
Expected Output:
Occurrences of 1: 3
Q29. Write a Rust program to concatenate two vectors of strings.
Input:
Vector 1: ["a", "b"], Vector 2: ["c", "d"]
Expected Output:
["a", "b", "c", "d"]
Q30. Write a Rust program to create a vector of vectors and flatten it into a single vector.
Input:
Vector of vectors: [[1, 2], [3, 4], [5, 6]]
Expected Output:
[1, 2, 3, 4, 5, 6]
Q31. Write a Rust program to create a vector of tuples and print each tuple.
Input:
Vector: [(1, "a"), (2, "b"), (3, "c")]
Expected Output:
(1, "a") (2, "b") (3, "c")
Q32. Write a Rust program to filter even numbers from a vector.
Input:
Vector: [1, 2, 3, 4, 5, 6]
Expected Output:
[2, 4, 6]
Q33. Write a Rust program to find the sum of the elements in a vector of floats.
Input:
Vector: [1.1, 2.2, 3.3]
Expected Output:
Sum: 6.6
Q34. Write a Rust program to find the product of the elements in a vector of integers.
Input:
Vector: [1, 2, 3, 4]
Expected Output:
Product: 24
Q35. Write a Rust program to create a vector of booleans and check if all values are true.
Input:
Vector: [true, true, false]
Expected Output:
Not all values are true.
Q36. Write a Rust program to remove the first element of a vector.
Input:
Vector: [1, 2, 3, 4]
Expected Output:
[2, 3, 4]
Q37. Write a Rust program to join the elements of a vector of strings into a single string.
Input:
Vector: ["Rust", "is", "awesome"]
Expected Output:
Rust is awesome
Q38. Write a Rust program to check if a vector of integers contains a sequence.
Input:
Vector: [1, 2, 3, 4], Sequence: [2, 3]
Expected Output:
Sequence found in the vector.
Q39. Write a Rust program to create a vector with default values.
Input:
Size: 5, Value: 10
Expected Output:
[10, 10, 10, 10, 10]
Q40. Write a Rust program to create a vector from a string and print each character.
Input:
String: "hello"
Expected Output:
h e l l o
Q41. Write a Rust program to rotate the elements of a vector by one position.
Input:
Vector: [1, 2, 3, 4]
Expected Output:
[2, 3, 4, 1]
Q42. Write a Rust program to find the intersection of two vectors.
Input:
Vector 1: [1, 2, 3], Vector 2: [2, 3, 4]
Expected Output:
[2, 3]
Q43. Write a Rust program to find the union of two vectors.
Input:
Vector 1: [1, 2, 3], Vector 2: [3, 4, 5]
Expected Output:
[1, 2, 3, 4, 5]
Q44. Write a Rust program to find the difference of two vectors.
Input:
Vector 1: [1, 2, 3], Vector 2: [2, 3, 4]
Expected Output:
[1]
Q45. Write a Rust program to shuffle the elements of a vector.
Input:
Vector: [1, 2, 3, 4]
Expected Output:
[4, 1, 3, 2] (or any random permutation)
Q46. Write a Rust program to create a vector and convert it to a HashSet.
Input:
Vector: [1, 2, 3, 4, 4, 5]
Expected Output:
HashSet: {1, 2, 3, 4, 5}
Q47. Write a Rust program to find the even-indexed elements in a vector.
Input:
Vector: [1, 2, 3, 4, 5]
Expected Output:
[1, 3, 5]
Q48. Write a Rust program to find the odd-indexed elements in a vector.
Input:
Vector: [1, 2, 3, 4, 5]
Expected Output:
[2, 4]
Q49. Write a Rust program to convert a vector to a set of strings with a specific prefix.
Input:
Vector: ["apple", "banana", "cherry"], Prefix: "fruit_"
Expected Output:
["fruit_apple", "fruit_banana", "fruit_cherry"]
Q50. Write a Rust program to partition a vector into two based on a condition.
Input:
Vector: [1, 2, 3, 4, 5], Condition: Even and Odd
Expected Output:
Even: [2, 4], Odd: [1, 3, 5]