Goldman Sachs Interview Question
Analysts@rajendra: (5,4,2,1,3) is also a set but not sorted.... Set is just a collection of distinct elements..
List is implemented as doubly link List. so we can push element at the back and front . similarly we can also pop element from back and front . complexity for push and pop is O(1).
Sets are containers which store only unique values . The values in the sets are stored in some specific order (like ascending or descending). compexity for inserting and searching any value is O(logN) .
List
- Anonymous February 23, 2012-Searching O(n)
-Inserting, deleting, moving O(1)
-Elements may be ordered. (order from which they are added)
-Elements may be sorted.
-Elements may be duplicate.
Set
-Searching (logarithmic in size).
-Insert and delete (logarithimic in general).
-Elements are un-ordered. (they are sorted on insert, doesn't keep the order it was inserted)
-Elements are always sorted from lower to higher.
-Elements are unique.
a set is always sorted, a list can be sorted on insert too depend on setting.
a set is kind of a subgroup of a list with more restriction but better performance.
Just remember that values are unique.
As far as I know a set is sorted, but I'm sure this is a debatable subject, might be some people that created set object/list that aren't sorted on insert... but as far as I know, a set is suppose to sort value on insert...