Published in

InterviewNoodle

# Array: A Well Known Data Structure

Hi Coder, Rubics this side. Today I decided to give you a dig-down analysis of everyone's favorite Data Structure “Array”. We go through some basic and Important questions/topics which are important for cracking any interview.

Rubics, is it worth it to read this blog?

Yes, Usually people know about Array, and its operations but only a few people know Why we need an array, and most important What are the issues with Array that force us to look into other Data structures.

# What is Array?

Ohh…come on Rubics! Not again, you told us so many times.

Yup, But let’s see again:

An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together.

Now Let’s see some important points:

• The array is the simplest, linear data structure.
• An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together.
• It is easier to calculate the location of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array.
• The location of the next index mainly depends on the data type of array. As each item takes its own space and the next space is allocated after it.
• Arrays can be declared in various ways in different languages. For illustration, let’s take the C array declaration.

As per the above illustration, the following are the important points to be considered.
1. Index starts with 0.
2. Array length is 10 which means it can store 10 elements.
3. Each element can be accessed via its index. Forex, we can fetch an element at index 6 as 27.

# Why do we need an Array?

The array is used to make things easy. Suppose you have to store all student's marks by the role number or a list of items. In a normal case, we have to care about a number of variables then we have to manage them.

That’s why we choose Array to make our job easy.

Ohk Rubics, then Why do we need other data structure, they also use to store a lot of continue data into single variable.

Yes, there are other data structures that store the same as an array but it's not always about storing the data but also caring about operations on data.

# Operations On Array:

There are mainly 4 types of operations that we need to perform:

1. Traverse
2. Insertion
3. Deletion
4. Searching & sorting

## Traversal of Array:

• The method of processing each element in the array exactly once is known as Traversal. In an array, Traversal starts from the first element in the array and ends at the last element of the array.
• Traversing an array means accessing every element of the array for a specific purpose. For example: printing every element, counting the total number of elements, or performing any process on these elements.
• Since, the array is a linear data structure (because all its elements form a sequence), traversing its elements is straightforward.

## Insertion:

• As the name suggests, insert data in an array but it is not as easy as it looks.
• Inserting in the last of the array needs to traverse the whole array if the length is unknown. and in the case of static memory, there is the limitation of upper bound which we need to care about.
• Inserting an item at starting or mid of the array needs to swift all values which makes this job tough.

## Deletion:

• As the insertion, deleting data in an array is not as easy as it looks.
• Deleting from the last of the array needs to traverse the whole array if the length is unknown.
• Deleting an item from starting or mid of the array needs to swift all values which make this job tough.

## Searching in Array:

• Not even a single day pass, when we do not have to search for something in our day-to-day life, car keys, books, pen, mobile charger, and whatnot. The same is the life of a computer, there is so much data stored in it, that whenever a user asks for some data, the computer has to search its memory to look for the data and make it available to the user. And the computer has its techniques to search through its memory fast, which you can learn more about in Operating System.
• What if you have to write a program to search a given number in an array? How will you do it?
• Well, to search for an element in a given array, there are two popular algorithms available:
1. Linear Search
2. Binary Search

1. Reading an array element is simple and efficient. This is because any element can be instantly read using indexes (base address calculation behind the scene) without traversing the whole array.

2. Array is a foundation of other data structures. For example, other data structures such as Linked List, Stack, Queue, etc. are implemented using an array.

3. All the elements of an array can be accessed using a single name (array name) along with the index, which is readable, userfriendly, and efficient rather than storing those elements in different-2 variables.

1. While using an array, we just need to decide the size of the array in the beginning, so if we are not aware of how many elements we are going to store in the array, it would make the task difficult.

2. The size of the array is fixed so if at a later point if we need to store more elements in it then it can’t be done. On the other hand, if we store less number of elements than the declared size, the remaining allocated memory is wasted.

# Applications of Array:

Apart from being widely used in programming, arrays have additional applications as well:

• Used in mathematical problems like matrices etc.
• They are used in the implementation of other data structures like linked lists, stacks, queues, etc.
• Database records are usually implemented as arrays.
• Used in lookup tables by computer.
• It effectively executes memory addressing logic wherein indices act as addresses to the one-dimensional array of memory.

If you are a beginner, you can start testing your coding skill in our code lab.

See You Soon!!

--

--