We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Ruby
  3. Arrays & Hashes
  4. Ruby Array - Addition

Ruby Array - Addition

Problem
Submissions
Leaderboard
Discussions

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)

Author

dheeraj

Difficulty

Easy

Max Score

10

Submitted By

20446

Need Help?


View discussions
View top submissions

rate this challenge

MORE DETAILS

Download problem statement
Download sample test cases
Suggest Edits
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy