Ruby Interview Questions

Want to become an expert in cracking Ruby interview questions?

Start with practicing the questions below. Whether a question involves multiple choice or live coding, we will give you hints as you go and tell you if your answers are correct or incorrect.

After that, take our timed public Ruby Interview Questions Test.

To use our service for testing candidates, buy a pack of candidates.


1. Common Names
Ruby Arrays Public New

Implement the find_common method. When passed two arrays of names, it will return an array containing the names that appear in either or both arrays. The returned array should have no duplicates.

For example, calling CommonNames.find_common(["Ava", "Emma", "Olivia"], ["Olivia", "Sophia", "Emma"]) should return an array containing Ava, Emma, Olivia, and Sophia in any order.

Easy 
10min
Ruby 2.5.1
 


  •   Example case: Wrong answer
  •   Each array has distinct names: Wrong answer
  •   Each array has duplicate names: Wrong answer
  •   Arrays have some names in common: Wrong answer

2. Palindrome
Ruby Strings Public

A palindrome is a word that reads the same backward or forward.

Write a function that checks if a given word is a palindrome. Character case should be ignored.

For example, is_palindrome("Deleveled") should return true as character case should be ignored, resulting in "deleveled", which is a palindrome since it reads the same backward and forward.

Easy  
10min
Ruby 2.5.1
 


  •   Example case: Wrong answer
  •   Lowercase words: Wrong answer
  •   Various words: Wrong answer

3. File Owners
Ruby Language Public

Implement a group_by_owners function that:

  • Accepts a hash containing the file owner name for each file name.
  • Returns a hash containing an array of file names for each owner name, in any order.

For example, for hash {'Input.txt' => 'Randy', 'Code.py' => 'Stan', 'Output.txt' => 'Randy'} the group_by_owners function should return {'Randy' => ['Input.txt', 'Output.txt'], 'Stan' => ['Code.py']}.

Easy  
15min
Ruby 2.5.1
 


  •   Example case: Wrong answer
  •   Each owner has a single file: Wrong answer
  •   Various files: Wrong answer

4. Pipeline
Ruby Closures Public New

As part of a data processing pipeline, complete the implementation of the pipeline method:

  • The method should accept a variable number of functions, and it should return a new function that accepts one parameter arg.
  • The returned function should call the first function in the pipeline with the parameter arg, and call the second function with the result of the first function.
  • The returned function should continue calling each function in the pipeline in order, following the same pattern, and return the value from the last function.

For example, Pipeline.pipeline(-> (x) { x * 3 }, -> (x) { x + 1 }, -> (x) { x / 2 }) then calling the returned function with 3 should return 5.

Hard 
10min
Ruby 2.5.1
 


  •   Example case: Wrong answer
  •   Various functions: Wrong answer
  •   Various data types: Wrong answer

5. Student Records
Ruby Lambda functions Splats Public New

Implement the StudentRecords.filter method. It should accept a list of students and a variable number of filters. The filters will be passed in as lambdas, each of which will take one student record and return a boolean value.

StudentRecords.filter should return a list of student records that have passed through all the filters.

If no filters are provided, the list of student records should be returned as is. If no students pass through all the provided filters (or if no students are provided), return an empty array.

For example, consider the following code:

students = [
  { name: 'Thomas Edison', gpa: 3.45 },
  { name: 'Grace Hopper', gpa: 3.99 },
  { name: 'Leonardo DaVinci', gpa: 2.78 }
]

honor_roll = ->(record) { record[:gpa] > 3.0 }

StudentRecords.filter(students, honor_roll) should return:

[{:name=>"Thomas Edison", :gpa=>3.45},{:name=>"Grace Hopper", :gpa=>3.99}]
Hard 
15min
Ruby 2.5.1
 


  •   Example case: Wrong answer
  •   No filters case and no matching filters case: Wrong answer
  •   One filter returns only relevant students: Wrong answer
  •   Multiple filters return only students who match all filters: Wrong answer

6. Path
Ruby Data structures Strings Public

Write a function that provides change directory (cd) function for an abstract file system.

Notes:

  • Root path is '/'.
  • Path separator is '/'.
  • Parent directory is addressable as '..'.
  • Directory names consist only of English alphabet letters (A-Z and a-z).
  • The function should support both relative and absolute paths.
  • The function will not be passed any invalid paths.
  • Do not use built-in path-related functions.

For example:

path = Path.new('/a/b/c/d')
path.cd('../x')
puts path.current_path

should display '/a/b/c/x'.

Hard  
30min
Ruby 2.5.1
 


  •   Example case: Wrong answer
  •   Selecting child directories: Wrong answer
  •   Selecting parent directories: Wrong answer
  •   Selecting complex paths: Wrong answer

If you feel ready, take one of our timed public Ruby Interview Questions tests:
  • HTML/CSS, JavaScript, Ruby, and SQL Online Test (Easy / Hard)
  • Ruby Online Test (Easy / Hard)
  • Ruby and SQL Online Test (Easy / Hard)
  • Ruby on Rails Online Test (Easy)
Not exactly what you are looking for? Go to our For Jobseekers section.