Array Interview Questions

Array is one of the most commonly used data structure in any programming language, we often use array in different form, here we look at some of the frequently asked array questions during interview.

Array structure and basic principal remain same in all the language, there may be some syntax difference, and python array has more advanced built-in methods compare to other programming language.

If you are fresher or want to see how array structure looks in different programming language, then here are some quick references.

Array data structure basic (must know)

No matter what language you use, some basic principal of array data type that will remain same in all the language.

  • Array index starts from zero, not one, first element will be always zero
  • Array is homogenous , means once you create an array of integer, you cannot store any other type of data (like string, date) in that array.
  • We can search element by index number of an array, but we cannot insert element by index number.
  • When we delete element from array using index number, that index value remains the same, only data gets deleted.
  • Most of the time, array we deal with, are single dimension array, but we can create two dimension and multi dimension array in some programming languages.

Array questions answers

Can we define an array without specifying the array size?

Yes, we can define an array without the array size, then keep adding element as required

var carList=new Array();

When we don’t define the size of an array, it becomes zero by default.

We can define array in multiple ways, look at following examples .

const stringArray = ['green', 'park', 'big', 'elephant', 'roaming'];

How to find a duplicate number in an integer array?

Finding duplicate item from array will have following process.

  • Sorting the array.
  • Then looping through the array and making a list of duplicate item (by comparing with earlier item in loop).
  • Then removing all duplicate items.
Below are two example of implementation.

How to remove all duplicate numbers from an array?

There are various way we can remove an item from array, here is a post about how to remove item from array with examples.

How to search an element from array?

There are different ways we can search an element from an array, some built-in methods like find, filter etc. find will get the first matching element, when filter will get all the matching elements.

const words = ['green', 'park', 'elephant', 'roaming'];
const result = words.filter(word => word.length > 5);
console.log(result);

here is an example of how to search an element from array with examples.

How to change value of a specified index position in an array?
We can retrieve any array element by using its index number, and then simply can assign new value.
var stringArray = ['green', 'park', 'elephant', 'roaming'];

stringArray[1]="jungle";

In above example we have changed the value of second element stringArray[1] from “park” to “jungle”.

Interview Question Answers | Fresher Job Interview Tips