DEV Community

Cover image for Quick Interview Crack (Unordered Set)
Nihal Islam
Nihal Islam

Posted on

Quick Interview Crack (Unordered Set)

Unordered set is a Data Structure in C++ which can contain different data in unordered manner. Now, why unordered set is in this interview section and why do we need an unordered set.

Let us look into a quick introduction of unordered set. It is basically implemented as hash table. So, every element in an unordered set is unique. As it is implemented as hash table, most operations takes time complexity O(1) except some rare cases it takes up to O(N) time. When we insert an element into an unordered set, it passes through a hash function and set the value into a hash table. As a result, the inserted element stored in unordered manner. That is why it is called unordered set.

Searching an element or comparing elements or finding duplicates are common code problem given in job Interviews. Unordered set is a good use of these kinds of problem which helps reduce time complexity of many operations. Instead of thinking about brute force, we can always check weather we can use a data structure to reduce time complexity of our problem.

Some of the commonly used methods of unordered set with time complexity of O(1) are insert(), find(), empty(), begin(), end(), size(), erase(), clear() etc.

Top comments (0)