WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Software Engineering · Basic · question 2 of 100

What is the difference between an array and a linked list? Explain their pros and cons.?

📕 Buy this interview preparation book: 100 Software Engineering questions & answers — PDF + EPUB for $5

Arrays and Linked Lists are the two most common data structures used in programming. Both are used to store collections of data, but they have significant differences in terms of their implementation, performance, flexibility and ease of use.

An array is a collection of elements of the same data type, which are stored in contiguous memory locations. Each element can be accessed using an index, which specifies the position of the element in the array. Arrays have a fixed size, which is determined at the time of declaration. Arrays are great when you know the number of elements you will be storing and when random access is a desirable property. An array is more preferable when the size of data is small, and the number of operations is more.

For example, let’s consider an array of integers:

int arr[10];

This creates an array of 10 integers. The elements can be accessed using the index notation:

arr[0] = 1; // assign 1 to the first element of the array
arr[1] = 2; // assign 2 to the second element of the array

On the other hand, a linked list is a data structure that contains a series of nodes, where each node points to the next node in the sequence. Each node contains data and a reference to the next node in the list. Linked lists have no fixed size, and elements can be added or removed dynamically. They are particularly useful when you don’t know the size of the data you will be storing or when you frequently need to insert or remove elements from the list.

For example, let’s consider a linked list of integers:

typedef struct node_t {
    int data;
    struct node_t *next;
} node_t;

node_t *head = NULL;

This creates an empty list. The nodes can be added to the list using the following code:

node_t *new_node = malloc(sizeof(node_t));
new_node->data = 1;
new_node->next = NULL;

if (head == NULL) {
    head = new_node;
} else {
    node_t *current = head;
    while (current->next != NULL) {
        current = current->next;
    }
    current->next = new_node;
}

One of the significant differences between arrays and linked lists lies in their performance. In an array, elements can be accessed randomly with constant time by using the index, whereas in a linked list, accessing an element requires iterating through the list from the beginning, which takes linear time. However, arrays have a fixed size, and resizing requires allocating a new array and copying data, whereas linked lists can be resized dynamically without moving the data.

Arrays are suitable for applications that require random access to elements and a fixed number of elements. Linked lists are suitable for applications that require dynamic resizing of the collection and frequent adding or removing of elements.

In conclusion, the choice between an array and a linked list depends on the specific purpose and requirements of the application. Each data structure has its pros and cons, and knowing their characteristics and limitations is essential for writing efficient and robust programs.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Software Engineering interview — then scores it.
📞 Practice Software Engineering — free 15 min
📕 Buy this interview preparation book: 100 Software Engineering questions & answers — PDF + EPUB for $5

All 100 Software Engineering questions · All topics