Discussion of Arrays during an interview

Ayaz Qureshi
4 min readJun 14, 2022

Arrays are a common topic in programming interviews. Arrays will appear in many problems as the dev progress through their coding career. Arrays are ubiquitous, which indicates that they will be used in every computer language. It comes in various computer languages, including C, C++, Java, Python, Perl, and Ruby.

I interviewed a developer with ~3 year of experience. Here were the question-answer and some other discussions we had about the array. I added some extra details to the answer [offcourse] while writing this blog.

— Can you tell me some pros and cons of using Arrays:

Array items can be sorted in multiple ways at the same time. Using the index, we can access any element in O(1) time.

This is followed by disadvantages as well:

You must indicate how many elements will be stored in your array ahead of time, and we cannot change the size of the array after it has been created. To fill in or close gaps, you must rearrange the other pieces, which requires worst-case O(n) time.

— So what if I create an array and don’t assign any value to it

If we don’t specify the values ourselves, Java will use the default values of 0 for byte, short, int, and long, 0.0 for float and double, false for boolean, and null for objects.

Okay then, How do you differentiate an Array and an Object?

An array creates a collection of data and keeps it in a single variable, whereas an object represents an entity with characteristics (called a property). We can access, alter, and delete items from objects using brackets and dots, whereas we can access and modify items in arrays using a range of built-in methods and zero-based indexing. Various loops (e.g. for, for…in, for…of, forEach()) can traverse across object properties and array entries.

— Can I write this: strArray.length()?

I think we can!

That’s wrong, we can’t! Google it!

— Why do we use “Dimension” and “Subscript” when we talk about arrays?

  • In an array, “Dimension” refers to the number of indices, or subscripts, required to specify an individual element. Dimensions and subscripts can be perplexing.
  • A subscript is a number, whereas a dimension describes the range of acceptable keys.
  • Each dimension of the array only requires one subscript.
  • For example, arr[10][5] is an array with two dimensions: one of size 10 and one of size 5. To address its elements, you’ll need two subscripts. One must be between 0 and 9, inclusive, and the other must be between 0 and 4.

That’s correct!

— Try comparing Arrays with Linked Lists
Size: Because data may only be stored in contiguous blocks of memory in an array, its size cannot be changed at runtime. However, because a linked list’s node structure allows each node to point to the next, its size can be readily changed, allowing data to existing at several (non-contiguous) addresses.

Memory allocation: Memory is allocated at build time for arrays, but memory is allocated at runtime for linked lists. A dynamically allocated array, on the other hand, allocates memory during runtime as well.

Memory efficiency: Because each node maintains a reference to the next node along with the data, the linked list consumes more memory for the same number of elements. When there is uncertainty about size or big changes in the size of data pieces, however, linked lists may utilize less memory overall than arrays.

Time to execute: To reach any element in a linked list, all preceding elements must be traversed, whereas elements in an array can be accessed simply using their index. As a result, some actions, like changing an element, are faster in arrays, while others, like inserting an element, are faster in linked lists.

— What is the best way to acquire the index of an array element?
A linear or binary search can be used to determine an element’s index.

— In an array with values ranging from 1 to 100, how do you identify the missing integer?

This question is frequently asked during interviews to measure your understanding of how programmers manipulate and troubleshoot arrays. Because the solution may be dependent on the array’s specific elements or structure, this question may also test your problem-solving skills. Providing solutions to all scenarios, in addition to demonstrating your flexibility and wide knowledge, might impress the interviewer.

Calculate the sum of the series using this function: n (n + 1) / 2 to find a missing integer. This function will only operate if there are no duplicates in the array or if it is missing more than one integer. If an array has duplicate elements, you can sort it to see if there are two elements that are equal.

— How can you get rid of a certain element from an array?

Because you can’t directly remove elements from the original array because arrays are fixed sets with no way to adjust their size, the interviewer is asking for you to come up with a different solution to the problem.

Creating a new array is the best technique to remove an element. You might include copies of the original array’s elements in this array while excluding only the element you want to remove.
Another method is to look for the target element in the array and then return all the items on the right side of the target element to their original location.

The developer was hired at last!

That's all for today folks!

--

--

Ayaz Qureshi

I write about startups, entrepreneurship mindset, strategies and app development.