DEV Community

seonglinchua
seonglinchua

Posted on

πŸ” DSA Interview Q&A – Day 1: Arrays vs Linked Lists

Welcome to Day 1 of the DSA Interview Q&A Series, where we dive deep into commonly asked data structures and algorithms questions to help you prepare smarter for technical interviews.


βœ… Question

What is the difference between an Array and a Linked List?


πŸ’‘ Answer

Arrays and Linked Lists are both linear data structures, but they differ in how they handle memory, operations, and performance.


πŸ”Έ Array

Feature Description
Memory Contiguous block of memory
Size Fixed size (defined at creation)
Access Time O(1) – Direct access via index
Insertion O(n) – Elements must shift
Deletion O(n) – Elements must shift

Use Case: Best when you need fast, direct access and the size of data doesn't change frequently.

Example (C#):

int[] arr = { 10, 20, 30, 40, 50 };
Console.WriteLine(arr[2]); // Output: 30
Enter fullscreen mode Exit fullscreen mode

πŸ”Έ Linked List

Feature Description
Memory Nodes are scattered in memory; each node points to the next
Size Dynamic – grows/shrinks as needed
Access Time O(n) – Must traverse from head
Insertion O(1) – If pointer to node is known
Deletion O(1) – If pointer to node is known

Use Case: Ideal when frequent insertions and deletions are required and random access is not needed.

Example (C#):

public class Node
{
    public int Data;
    public Node Next;
}

Node head = new Node { Data = 10, Next = null };
head.Next = new Node { Data = 20, Next = null };

Console.WriteLine(head.Next.Data); // Output: 20
Enter fullscreen mode Exit fullscreen mode

🧠 Key Comparison

Feature Array Linked List
Memory Layout Contiguous Non-contiguous
Size Flexibility Fixed Dynamic
Access O(1) O(n)
Insertion O(n) O(1)*
Deletion O(n) O(1)*

*If pointer/reference to the node is already known.


🎯 Interview Tip

  • Prefer arrays when you need fast indexing and minimal insert/delete operations.
  • Use linked lists when your program needs frequent modifications (insert/delete) in the middle of the structure.

πŸ‘‰ Up Next: How to detect a cycle in a linked list (Floyd’s Algorithm)

πŸ”” Follow for daily interview prep content and real-world examples!

πŸ’¬ Comment below your experience or tips using arrays and linked lists in projects.

Top comments (0)