Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerRank Ruby Array – Index, Part 1 problem solution

YASH PAL, 31 July 202429 January 2026

In this HackerRank Ruby Array – Index, Part 1 problem-solution Array collections offer various ways to access their elements. The positions are 0 indexed. Objects of the array can be accessed using the [] method which may take various arguments, as explained below.

arr = [9, 5, 1, 2, 3, 4, 0, -1]

A number which is the position of an element

>>arr[4]

  => 3

or


>>arr.at(4)

  => 3 

A range indicating the start and the end position

>>arr[1..3] # .. indicates both indices are inclusive. 

  => [5,1,2]

>>arr[1…3] # … indicates the last index is excluded.

  => [5,1]

Start index and the length of the range

>>arr[1,4]

  => [5, 1, 2, 3]

HackerRank Ruby Array - Index, Part 1 problem solution

HackerRank Ruby Array – Index, Part 1 problem solution.

def element_at(arr, index)
    arr[index]
end

def inclusive_range(arr, start_pos, end_pos)
    arr[start_pos..end_pos]
end

def non_inclusive_range(arr, start_pos, end_pos)
    arr[start_pos...end_pos]
end

def start_and_length(arr, start_pos, length)
    arr[start_pos,length]
end

Second solution.

def element_at(arr, index)
    # return the element of the Array variable `arr` at the position `index`
    # arr.at(index) # or
    arr[index]
end

def inclusive_range(arr, start_pos, end_pos)
    # return the elements of the Array variable `arr` between the start_pos and end_pos (both inclusive)
    arr[start_pos,(end_pos-start_pos+1)]
end

def non_inclusive_range(arr, start_pos, end_pos)
    # return the elements of the Array variable `arr`, start_pos inclusive and end_pos exclusive
    arr[start_pos,end_pos-start_pos]
end

def start_and_length(arr, start_pos, length)
    # return `length` elements of the Array variable `arr` starting from `start_pos`
    arr[start_pos,length]
end

coding problems solutions Hackerrank Problems Solutions Ruby Solutions HackerRankruby

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

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