Javatpoint Logo
Javatpoint Logo

Design a data structure that supports insert, delete, search, and getRandom in constant time

Designing a data structure that allows for constant-time insertion, deletion, searching, and random access is an interesting issue in computer science. Obtaining consistent time complexity for these activities sometimes necessitates a trade-off between various characteristics of data storage and access. This article delves into the core principles and potential implementations of several tactics and approaches for building such a data structure.


Data structures are the foundation of effective computing. The pursuit of a data structure with constant temporal complexity for critical operations, including insertion, deletion, search, and random access, has been a focus of algorithmic design. Traditional data structures such as arrays, linked lists, trees, and hash tables each have their advantages, but they often need to give constant time for all of these operations to be performed concurrently. Innovative ways that combine the qualities of these structures are frequently used to accomplish this accomplishment.


Before getting into the design, it's critical to understand the data structure's requirements and constraints:

  1. Constant Time Complexity: All operations, including insertion, deletion, search, and random access, should be executed in constant time, indicated as O(1).
  2. Dynamic Resizing: The data structure should be able to manage dynamic resizing to accommodate fluctuating quantities of items.
  3. Support for Random Access: It is critical to be able to access a random element in constant time.
  4. No Specific Order Requirement: There is no specific order requirement. Elements cannot be stored in the same order as arrays or lists.

Possible Solutions

Hash Map and Array/List Hash Table

In most cases, inserting, deleting, and searching a typical hash table takes O(1) time. However, due to the nature of hash functions, obtaining constant time for random access takes time. A hash table combined with an array or list that allows for random access may fulfill the requirements.

Tree with Additional Indexing that is Balanced

For most operations, balanced trees such as AVL or Red-Black trees offer O(log n) time complexity. Including an extra indexing mechanism (such as an array of pointers) might allow for constant-time random access.

Hash Map Resizable Array

A resizable array (such as a list in Python or a vector in C++) can enable constant-time random access. Combining it with a hash map that contains array indices might theoretically offer constant time for all operations.

Design: Resizable Array + Hash Map

Let's look at a possible solution that combines a resizable array with a hash map to suit the requirements:

Data Structure Elements:

Resizable Array (ArrayList): An array that modifies its size dynamically to accept items.

Hash Map (HashMap): Maps components to their array indices.


Insertion (insert(element)):

  • Append the element to the resizable array.
  • Store the index of the newly added element in the hash map with the element as the key.

Deletion (delete(element)):

  • Find the index of the element in the hash map.
  • Remove the element from the array and update indices in the hash map accordingly.

Search (search(element)):

  • Retrieve the index of the element from the hash map and return the element at that index in the array.

GetRandom (getRandom()):

  • Generate a random index within the current size of the array.
  • Return the element to the generated index.

Analysis of Complexity

Insertion, deletion, search, and GetRandom: Each of these actions requires constant-time access to the hash map and the array, ensuring that the O(1) complexity condition is met.



Design a data structure that supports insert, delete, search, and getRandom in constant time


  • ConstantTimeDS holds numbers in two ways: an array and a map-like system (hash_map) to find numbers in the array quickly.
  • Size keeps track of how many numbers are in the structure.
  • initializeDS: Sets up the structure initially, creating a space for numbers.
  • Insert: Adds a number to the structure if it's not already there.
  • Delete: Removes a number from the structure.
  • Search: Check if a number exists in the structure.
  • getRandom: Retrieves a random number from the existing ones.
  • Insert and delete: Ensure the structure doesn't exceed a certain size and maintain uniqueness.
  • Search: Checks if a number is present.
  • getRandom: Picks a random number from the available ones.
  • Limited to a maximum number of elements (set by MAX_SIZE).
  • It is not suitable for large-scale applications due to the fixed size.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA