Common patterns: iteration, hashing, two-pointer, basic transformations. Try each yourself first – ask for solutions/hints per question when ready.
- Reverse a string without using
.reversereverse_string("hello") => "olleh" - Palindrome check
palindrome?("racecar") => truepalindrome?("hello") => false - Count vowels
count_vowels("programming") => 3 - Find max in array without
.maxfind_max([3, 7, 2, 9, 4]) => 9 - Remove duplicates from array, preserve order
remove_duplicates([1,2,2,3,1,4]) => [1,2,3,4] - FizzBuzz (1 to n)
fizzbuzz(15) => ["1","2","Fizz","4","Buzz",...,"FizzBuzz"] - Anagram check
anagram?("listen", "silent") => true - Sum of array, no
.sumarray_sum([1,2,3,4]) => 10 - Capitalize each word (title case), no
.capitalizeon whole stringtitle_case("the ruby language") => "The Ruby Language" - Find second largest number
second_largest([4, 1, 9, 7, 9]) => 7
1. Reverse a string without using .reverse
Concept
1. finding the last string character index to find the last string character first
2. then decreasing the index to find the upto the first character
def reverse_string(str)
last_str_index = str.length - 1
result = ""
while last_str_index >= 0
result << str[last_str_index]
last_str_index -= 1
end
result
end
puts reverse_string("hello")
puts reverse_string("programming")
Solution 2: Another way without using Index variables
def reverse_string(str)
str.each_char.reduce("") { |result, char| char + result }
end
Concept
Prepend each character to an accumulator instead of appending – that flips the order without touching any index. each_char + reduce replaces the while-loop/counter entirely.
2. Palindrome check
def palindrome?(str)
first_char_index = 0
last_char_index = str.length - 1
while first_char_index < last_char_index
if str[first_char_index] != str[last_char_index]
return false
end
first_char_index += 1
last_char_index -= 1
end
return true
end
p palindrome?("ala")
p palindrome?("alla")
p palindrome?("racecar")
p palindrome?("car")
Concept
- Two pointer approach
Two indices start at opposite ends of the string and move toward each other, comparing elements pairwise:
first_char_indexstarts at0,last_char_indexstarts atlength - 1- Each iteration compares
str[first]vsstr[last]– if they ever mismatch, it can’t be a palindrome, so return immediately - Otherwise, both pointers move inward (
first += 1,last -= 1) until they meet or cross (first < lastbecomes false) - If the loop finishes without a mismatch, all mirrored pairs matched → palindrome
Why this pattern in general: it’s the go-to when you need to compare elements from both ends of a sequence without extra space – palindromes, reversing in-place, “sorted array pair sum” problems, container/water-trapping problems all reuse this exact skeleton (two indices, converge or diverge, one comparison per step).
One edge case worth saying out loud in an interview: this correctly handles even-length (abba) and odd-length (aba, middle char never gets compared to itself) without any special-casing – that’s often a follow-up question.
Count vowels
p count_vowels("programming")