Bird
Raised Fist0
Rubyprogramming~20 mins

Array sorting and reversing in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Array Sorting and Reversing Master
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?
Consider the following Ruby code snippet. What will it print?
Ruby
arr = [5, 3, 8, 1]
puts arr.sort.reverse.join(",")
A"1,3,5,8"
B"8,5,3,1"
C"5,3,8,1"
D"8,3,5,1"
Attempts:
2 left
💡 Hint
Think about what sort and reverse do to the array.
❓ Predict Output
intermediate
2:00remaining
What does this Ruby code print?
Look at this Ruby code. What will be printed?
Ruby
arr = ["dog", "cat", "bird"]
puts arr.sort.reverse[0]
A"error"
B"bird"
C"dog"
D"cat"
Attempts:
2 left
💡 Hint
Sort alphabetically, then reverse, then pick first element.
🔧 Debug
advanced
2:00remaining
Why doesn't this Ruby code sort and reverse the original array?
This Ruby code is intended to sort and reverse the original array in place, but the original array remains unchanged. What is the cause?
Ruby
arr = [1, 2, 3]
arr.sort.reverse!
puts arr
Asort returns a new array; reverse! modifies the new array but leaves the original unchanged
BNo error; code runs and prints "3\n2\n1"
Creverse! is not a valid method in Ruby
Dreverse! returns nil and causes puts to print nothing
Attempts:
2 left
💡 Hint
Check what sort and reverse! do to the array.
🧠 Conceptual
advanced
2:00remaining
How many elements are in the array after this Ruby code?
What is the number of elements in the array after running this code?
Ruby
arr = [4, 7, 2, 9]
new_arr = arr.sort.reverse.select { |x| x > 5 }
A1
B3
C4
D2
Attempts:
2 left
💡 Hint
Sort, reverse, then select elements greater than 5.
❓ Predict Output
expert
3:00remaining
What is the output of this Ruby code with mixed types?
What will this Ruby code print?
Ruby
arr = ["10", 2, "3", 1]
sorted = arr.sort_by { |x| x.to_i }.reverse
puts sorted.join("-")
A"10-3-2-1"
B"error"
C"10-2-3-1"
D"3-10-2-1"
Attempts:
2 left
💡 Hint
Convert all elements to integers for sorting, then reverse.

Practice

(1/5)
1. What does the Ruby method sort do when called on an array?
easy
A. It arranges the elements in ascending order.
B. It reverses the order of elements.
C. It removes duplicate elements.
D. It shuffles the elements randomly.

Solution

  1. Step 1: Understand the purpose of sort

    The sort method arranges elements in ascending order by default.
  2. Step 2: Compare with other methods

    Unlike reverse which flips order, sort orders elements from smallest to largest.
  3. Final Answer:

    It arranges the elements in ascending order. -> Option A
  4. Quick Check:

    sort = ascending order [OK]
Hint: Remember: sort arranges ascending, reverse flips order [OK]
Common Mistakes:
  • Confusing sort with reverse
  • Thinking sort removes duplicates
  • Assuming sort shuffles elements
2. Which of the following is the correct syntax to reverse an array arr in Ruby?
easy
A. arr.reverse!
B. arr.reverse[]
C. reverse.arr()
D. arr.reverse()!

Solution

  1. Step 1: Identify the correct method call

    The method to reverse an array in place is reverse! called on the array object.
  2. Step 2: Check syntax correctness

    Only arr.reverse! is valid Ruby syntax; others are incorrect method calls or misplaced symbols.
  3. Final Answer:

    arr.reverse! -> Option A
  4. Quick Check:

    reverse! modifies array in place [OK]
Hint: Use reverse! to reverse array in place [OK]
Common Mistakes:
  • Using parentheses incorrectly
  • Placing exclamation mark after parentheses
  • Calling reverse as a standalone function
3. What is the output of the following Ruby code?
arr = [3, 1, 4, 2]
sorted_arr = arr.sort
reversed_arr = sorted_arr.reverse
puts reversed_arr.inspect
medium
A. [1, 2, 3, 4]
B. [3, 1, 4, 2]
C. [4, 3, 2, 1]
D. [2, 4, 1, 3]

Solution

  1. Step 1: Sort the original array

    Calling arr.sort returns a new array sorted ascending: [1, 2, 3, 4].
  2. Step 2: Reverse the sorted array

    Calling sorted_arr.reverse flips the order to [4, 3, 2, 1].
  3. Final Answer:

    [4, 3, 2, 1] -> Option C
  4. Quick Check:

    sort then reverse = descending order [OK]
Hint: Sort then reverse = descending order [OK]
Common Mistakes:
  • Assuming reverse modifies original array
  • Confusing sorted array with original
  • Expecting reverse to sort ascending
4. Identify the error in this Ruby code snippet:
arr = [5, 2, 9]
arr.sort!
arr.reverse
puts arr.inspect
medium
A. Missing parentheses in method calls cause errors.
B. The sort! method is invalid syntax.
C. The array arr cannot be reversed after sorting.
D. Using reverse does not change arr in place.

Solution

  1. Step 1: Understand sort! effect

    sort! sorts the array arr in place, so arr becomes [2, 5, 9].
  2. Step 2: Check reverse method usage

    reverse returns a new reversed array but does not modify arr itself.
  3. Final Answer:

    Using reverse does not change arr in place. -> Option D
  4. Quick Check:

    reverse without ! returns new array [OK]
Hint: Use reverse! to modify array in place [OK]
Common Mistakes:
  • Assuming reverse changes original array
  • Thinking sort! is invalid
  • Believing method calls need parentheses always
5. Given an array arr = [7, 3, 5, 3, 9, 1], how can you create a new array that is sorted in descending order without changing the original array?
hard
A. Use arr.sort! then arr.reverse!
B. Use arr.sort.reverse
C. Use arr.reverse.sort
D. Use arr.reverse! then arr.sort!

Solution

  1. Step 1: Sort the array without modifying original

    arr.sort returns a new sorted array ascending, original arr stays unchanged.
  2. Step 2: Reverse the sorted array to get descending order

    Calling reverse on the sorted array returns a new array reversed, still not changing original.
  3. Final Answer:

    Use arr.sort.reverse -> Option B
  4. Quick Check:

    sort then reverse = descending new array [OK]
Hint: Chain sort then reverse to get descending without changing original [OK]
Common Mistakes:
  • Using sort! or reverse! which modify original array
  • Reversing before sorting
  • Expecting reverse.sort to work the same
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