Arrays, linked lists, and hash tables are fundamental data structures in computer science. Arrays store elements in contiguous memory locations, allowing fast access by index but fixed size. Linked lists consist of nodes connected by pointers, enabling dynamic memory allocation and easy insertion or deletion but slower access. Hash tables use a hash function to map keys to values, providing efficient data retrieval, insertion, and deletion, though their performance depends on the hash function and collision handling.
Arrays, linked lists, and hash tables are fundamental data structures in computer science. Arrays store elements in contiguous memory locations, allowing fast access by index but fixed size. Linked lists consist of nodes connected by pointers, enabling dynamic memory allocation and easy insertion or deletion but slower access. Hash tables use a hash function to map keys to values, providing efficient data retrieval, insertion, and deletion, though their performance depends on the hash function and collision handling.
What is an array and what are its main advantages and limitations?
An array stores elements in contiguous memory for O(1) index access, but has a fixed size. Resizing is costly and may waste space.
What is a linked list and how does it differ from an array?
A linked list is a sequence of nodes connected by pointers, growing/shrinking dynamically. It supports efficient insertions/deletions, but random access by index is O(n).
What is a hash table and how does it achieve fast lookups?
A hash table uses a hash function to map keys to buckets, giving average O(1) lookups and inserts. Collisions are handled by chaining or open addressing.
When should you choose each data structure?
Use an array for fast random access with a known size; a linked list for dynamic size and frequent insertions/deletions; a hash table for fast key-based lookups.