DEV Community

Cover image for Master Array Data structure.
Raksha Kannusami
Raksha Kannusami

Posted on • Updated on

Master Array Data structure.

Most interviews for an SDE (software development engineer) role require you to have a good grasp of Data structures and algorithms.

One important data structure is Arrays.

An array is a linear data structure and is famous for storing values at contiguous memory locations.

This article will help you to get started with arrays. 🥳

I will be using Java, but don't worry, you should be able to understand these pseudo-codes irrespective of programming language.

Let us understand the basic manipulation on arrays like insert, delete, sort, etc.

1. Create an array.

To create an integer array of size 100,

int[] array = new int[100];
Enter fullscreen mode Exit fullscreen mode

2. Insert an element into an array.

The following function will insert any element at any position specified by the user and will return the size of the modified array after insertion.

int insert(int array[], int n, int x, int capacity, int position)
{
  if(n==capacity)
    return n; //cannot insert any element since exceeding capacity
  int index = position-1;
  for(int i=n-1; i>=index;i--)
    array[i+1]=array[i];
  array[index]=x;
  return (n+1); //returns the size of the new array.
}
Enter fullscreen mode Exit fullscreen mode

3. Delete an element from an array.

The following function will delete any element at the position specified by the user and will return the size of the modified array after deletion.

int delete(int array[],int n, int x)
{
  int i;
  for(int i=0;i<n;i++)
    if(array[i]==x)
      break;
    if (i==n)
      return n; // the element is not present in the array
    for(int j=i;j<n-1;j++)
      array[j]=array[j+1];
    return (n-1); // returns the size of array after deletion
}
Enter fullscreen mode Exit fullscreen mode

4. Sort an array (Ascending order).

Java has an arrays class which can be used to sort arrays by importing java.util.Arrays.

Arrays.sort(array);
Enter fullscreen mode Exit fullscreen mode

These were the basic manipulation of arrays that can get you started with. If you want to dive deeper and master them to crack technical interviews, then try to build your logic to solve the following questions:

  1. How to reverse an array?
  2. How to find leaders in an array?
  3. How to left rotate an array?
  4. What is kadane's algorithm?
  5. What is the sliding window technique?
  6. How to solve the Trapping rainwater problem?
  7. How to remove duplicate elements in an array?
  8. How to find subarrays for a given condition?

You can find the pseudo-codes to the above questions my Github repository. (Pssst... do give it a star if you found it helpful) 😄

If you can explore the above-listed problems and as well as solve the questions on your favorite coding platform, that should give you enough confidence to crack any type of coding question based on arrays. 🎉

To get started with, try Hackerrank or Hackerearth. 👩🏻‍💻

Happy learning! happy coding! 💖

Top comments (0)