Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerRank Ruby Array – Addition problem solution

YASH PAL, 31 July 2024

In this HackerRank Ruby Array – Addition problem solution Arrays provide a variety of methods that allow to add elements to them.

push allows one to add an element to the end of the list.

 >x = [1,2]

 >x.push(3)

 => [1,2,3]

insert allows one to add one or more elements starting from a given index (shifting elements after the given index in the process).

 >x = [1,2]

 >x.insert(1, 5, 6, 7)

 => [1, 5, 6, 7, 2]

unshift allows one or more elements to be added at the beginning of the list.

 >x = [1,2,3]

 >x.unshift(10, 20, 30)

 => [10, 20, 30, 1, 2, 3]

In this challenge, your task is to complete three functions that take in the array arr and

  1. Add an element to the end of the list
  2. Add an element to the beginning of the list
  3. Add an element after a given index (position)
  4. Add more than one element after a given index (position)

HackerRank Ruby Array - Addition problem solution

Problem solution.

def end_arr_add(arr, element)
    # Add `element` to the end of the Array variable `arr` and return `arr`
    arr.push(element)
end

def begin_arr_add(arr, element)
    # Add `element` to the beginning of the Array variable `arr` and return `arr`
    arr.unshift(element)
end

def index_arr_add(arr, index, element)
    # Add `element` at position `index` to the Array variable `arr` and return `arr`
    arr.insert(index, element)
end

def index_arr_multiple_add(arr, index)
    # add any two elements to the arr at the index
    arr.insert(index, 4, 2)
end

Second solution.

def end_arr_add(arr, element)
    # Add `element` to the end of the Array variable `arr` and return `arr`
    arr.push( element )
end

def begin_arr_add(arr, element)
    # Add `element` to the beginning of the Array variable `arr` and return `arr`
    arr.unshift( element )
end

def index_arr_add(arr, index, element)
    # Add `element` at position `index` to the Array variable `arr` and return `arr`
    arr.insert( index, element )
end

def index_arr_multiple_add(arr, index)
    # add any two elements to the arr at the index
    arr.insert( index, 'foo', 'bar' )
end

coding problems solutions Ruby Solutions

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes