0
0
Rubyprogramming~20 mins

Array comparison and set operations in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array and Set Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Ruby code using array intersection?
Consider the following Ruby code snippet. What will it print?
Ruby
a = [1, 2, 3, 4]
b = [3, 4, 5, 6]
puts (a & b).inspect
A[5, 6]
B[1, 2, 3, 4, 5, 6]
C[3, 4]
D[1, 2]
Attempts:
2 left
💡 Hint
The & operator returns elements common to both arrays.
Predict Output
intermediate
2:00remaining
What does this Ruby code print when using array difference?
Look at this Ruby code. What is the output?
Ruby
a = [1, 2, 3, 4]
b = [3, 4, 5, 6]
puts (a - b).inspect
A[1, 2]
B[3, 4]
C[1, 2, 3, 4, 5, 6]
D[5, 6]
Attempts:
2 left
💡 Hint
The - operator removes elements from the first array that appear in the second.
Predict Output
advanced
2:00remaining
What is the output of this Ruby code using array union?
What will this Ruby code print?
Ruby
a = [1, 2, 3]
b = [3, 4, 5]
puts (a | b).inspect
A[3, 4, 5]
B[1, 2, 3, 4, 5]
C[1, 2]
D[1, 2, 3, 3, 4, 5]
Attempts:
2 left
💡 Hint
The | operator returns all unique elements from both arrays.
Predict Output
advanced
2:00remaining
What error does this Ruby code raise when comparing arrays with a non-array?
What error will this Ruby code produce?
Ruby
a = [1, 2, 3]
b = 5
puts (b & a).inspect
AArgumentError: wrong number of arguments (given 1, expected 0)
BTypeError: no implicit conversion of Integer into Enumerator
CTypeError: no implicit conversion of Integer into Array
DNoMethodError: undefined method `&' for Integer
Attempts:
2 left
💡 Hint
The & operator expects both operands to be arrays.
🧠 Conceptual
expert
3:00remaining
How many unique elements are in the result of this Ruby array operation?
Given the arrays below, how many elements are in the array resulting from (a | b) - (a & b)?
Ruby
a = [1, 2, 3, 4, 5]
b = [4, 5, 6, 7]
result = (a | b) - (a & b)
puts result.size
A5
B4
C6
D7
Attempts:
2 left
💡 Hint
Think about the symmetric difference: elements in either array but not both.