Bird
Raised Fist0
Rubyprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1.

Which operator in Ruby returns the common elements between two arrays?

[1, 2, 3] ? [2, 3, 4]
easy
A. & (intersection)
B. | (union)
C. - (difference)
D. == (equality)

Solution

  1. Step 1: Understand the operators

    The & operator returns elements common to both arrays.
  2. Step 2: Apply to example arrays

    For arrays [1, 2, 3] and [2, 3, 4], common elements are 2 and 3.
  3. Final Answer:

    & (intersection) -> Option A
  4. Quick Check:

    Common elements = & [OK]
Hint: Use & for common items between arrays [OK]
Common Mistakes:
  • Confusing union (|) with intersection (&)
  • Using - for common elements
  • Thinking == compares elements regardless of order
2.

Which of the following is the correct syntax to find the difference between two arrays a and b in Ruby?

easy
A. a == b
B. a - b
C. a | b
D. a & b

Solution

  1. Step 1: Recall difference operator

    The - operator returns elements in a not in b.
  2. Step 2: Confirm syntax correctness

    Using a - b is the correct Ruby syntax for difference.
  3. Final Answer:

    a - b -> Option B
  4. Quick Check:

    Difference = - [OK]
Hint: Use a - b to get items in a but not in b [OK]
Common Mistakes:
  • Using & instead of - for difference
  • Using | which is union
  • Using == which checks equality
3.

What is the output of the following Ruby code?

a = [1, 2, 3, 4]
b = [3, 4, 5, 6]
result = a | b
puts result.inspect
medium
A. [1, 2, 3, 4, 5, 6]
B. [3, 4]
C. [1, 2]
D. [1, 2, 3, 4]

Solution

  1. Step 1: Understand the union operator

    The | operator combines all unique elements from both arrays.
  2. Step 2: Apply union to arrays

    Arrays a and b combined uniquely are [1, 2, 3, 4, 5, 6].
  3. Final Answer:

    [1, 2, 3, 4, 5, 6] -> Option A
  4. Quick Check:

    Union = | [OK]
Hint: Use | to combine unique elements from both arrays [OK]
Common Mistakes:
  • Confusing union with intersection
  • Expecting duplicates in output
  • Using - which removes elements
4.

Identify the error in the following Ruby code snippet:

a = [1, 2, 3]
b = [2, 3, 4]
result = a & b
puts result

What is the problem?

medium
A. Syntax error due to missing parentheses
B. Outputs all elements from both arrays
C. Runtime error because & is not defined for arrays
D. No error; code runs and outputs common elements

Solution

  1. Step 1: Check syntax and operators

    The & operator is valid for arrays to find intersection.
  2. Step 2: Verify output behavior

    The code outputs common elements [2, 3] without error.
  3. Final Answer:

    No error; code runs and outputs common elements -> Option D
  4. Quick Check:

    Intersection & works fine [OK]
Hint: & works for arrays to find common items without syntax errors [OK]
Common Mistakes:
  • Thinking & causes syntax error
  • Expecting union instead of intersection
  • Confusing output with difference
5.

You have two arrays representing user IDs:

active_users = [101, 102, 103, 104, 105]
inactive_users = [103, 105, 107]

Which Ruby expression gives you the list of users who are active but not inactive?

hard
A. active_users | inactive_users
B. active_users & inactive_users
C. active_users - inactive_users
D. active_users == inactive_users

Solution

  1. Step 1: Understand the problem

    We want users in active_users but not in inactive_users.
  2. Step 2: Use difference operator

    The - operator returns elements in first array not in second.
  3. Step 3: Apply to given arrays

    active_users - inactive_users = [101, 102, 104]
  4. Final Answer:

    active_users - inactive_users -> Option C
  5. Quick Check:

    Difference = - [OK]
Hint: Use - to exclude inactive from active users [OK]
Common Mistakes:
  • Using & which gives only common users
  • Using | which combines all users
  • Using == which checks equality
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard