vinod's erudition

we learn everything from failure not from success so keep failing

Home Ruby Rails Javascript Python Agentic AI

Data Structure Reversal Array - Python

Posted on April 28, 2025 by vinod

Reverse an Array - My First Data Structure Problem on HackerRank!


Problem Statement

The task was simple yet fundamental:

Given an array of integers, reverse the order of the elements and return the reversed array.

This was my first data structure problem solved on HackerRank, and it helped me understand array manipulation basics.


My Python Solution

Here is the code I wrote to solve it:

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the 'reverseArray' function below.
# The function is expected to return an INTEGER_ARRAY.
# The function accepts INTEGER_ARRAY a as parameter.

def reverseArray(a):
    # Create an empty list to store the reversed elements
    a_reverse = []
    # Iterate from the last index to the first
    for i in range(len(a) - 1, -1, -1):
        a_reverse.append(a[i])
    return a_reverse

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    arr_count = int(input().strip())
    arr = list(map(int, input().rstrip().split()))

    res = reverseArray(arr)

    fptr.write(' '.join(map(str, res)))
    fptr.write('\n')

    fptr.close()

Step-by-Step Explanation

1. Input Handling

2. Logic to Reverse the Array

3. Output Handling


Key Python Concepts I Learned


Reflection

This problem gave me my first taste of how important array manipulation is in solving real-world problems. Even though it was simple, it laid the foundation for understanding more complex data structure operations like:

I’m excited to solve more such problems and build a solid understanding of Data Structures and Algorithms (DSA)!


Final Output Example

Input:

5
1 2 3 4 5

Output:

5 4 3 2 1

Stay tuned as I continue this DSA learning journey! 🚀

“Success is the sum of small efforts, repeated day in and day out.” - Robert Collier