set interview questions
Top set frequently asked interview questions
I have two lists in Python, like these:
temp1 = ['One', 'Two', 'Three', 'Four']
temp2 = ['One', 'Two']
I need to create a third list with items from the first list which aren't present in the second one. From the example I have to get:
temp3 = ['Three', 'Four']
Are there any fast ways without cycles and checking?
Source: (StackOverflow)
I am using this code to convert a Set
to a List
:
Map<String, List> mainMap = new HashMap<String, List>();
for(int i=0; i<something.size(); i++){
Set set = getSet(...); //return different result each time
List listOfNames = new ArrayList(set);
mainMap.put(differentKeyName,listOfNames);
}
I want to avoid creating a new list in each iteration of the loop. Is that possible?
Source: (StackOverflow)
Given Zero Piraeus' answer to another question, we have that
x = tuple(set([1, "a", "b", "c", "z", "f"]))
y = tuple(set(["a", "b", "c", "z", "f", 1]))
print(x == y)
Prints True
about 85% of the time with hash randomization enabled. Why 85%?
Source: (StackOverflow)
The powerset of {1, 2, 3}
is:
{{}, {2}, {3}, {2, 3}, {1, 2}, {1, 3}, {1, 2, 3}, {1}}
Let's say I have a Set
in Java:
Set<Integer> mySet = new HashSet<Integer>();
mySet.add(1);
mySet.add(2);
mySet.add(3);
Set<Set<Integer>> powerSet = getPowerset(mySet);
How do I write the function getPowerset, with the best possible order of complexity?
(I think it might be O(2^n).)
Source: (StackOverflow)
I am currently doing this:
Set<String> listOfTopicAuthors = ....
List<String> list = Arrays.asList(
listOfTopicAuthors.toArray( new String[0] ) );
Can you beat this ?
Source: (StackOverflow)
Iterator ite = Set.iterator();
Iterator ite = List.iterator();
ListIterator listite = List.listIterator();
We can use Iterator
to traverse a Set
or a List
or a Map
. But ListIterator
can only be used to traverse a List
, it can't traverse a Set
. Why?
I know that the main difference is that with iterator we can travel in only one direction but with ListIterator
we can travel both directions. Are there any other differences? And any advantages of ListIterator
over Iterator
?
Source: (StackOverflow)
Suppose the following:
>>>s = set([1, 2, 3])
How do I get a value (any value) out of s without doing s.pop()? I want to leave the item in the set until I am sure I can remove it - something I can only be sure of after an asynchronous call to another host.
Quick and dirty:
>>>elem = s.pop()
>>>s.add(elem)
But do you know of a better way? Ideally in constant time.
Source: (StackOverflow)
How do I pick a random element from a set?
I'm particularly interested in picking a random element from a
HashSet or a LinkedHashSet, in Java.
Solutions for other languages are also welcome.
Source: (StackOverflow)
When should I use a dictionary, list or set?
Are there scenarios that are more suited for each data type?
Source: (StackOverflow)
I would like to convert an array to a Set in Java. There are some obvious ways of doing this (i.e. with a loop) but I would like something a bit neater, something like:
java.util.Arrays.asList(Object[] a);
Any ideas?
Source: (StackOverflow)
Does anyone know if there is a good equivalent to Java's Set
collection in C#? I know that you can somewhat mimic a set using a Dictionary
or a HashTable
by populating but ignoring the values, but that's not a very elegant way.
Source: (StackOverflow)
Basically, I'm trying to create an object of unique objects, a set. I had the brilliant idea of just using a JavaScript object with objects for the property names. Such as,
set[obj] = true;
This works, up to a point. It works great with string and numbers, but with other objects, they all seem to "hash" to the same value and access the same property. Is there some kind of way I can generate a unique hash value for an object? How do strings and numbers do it, can I override the same behavior?
Source: (StackOverflow)
How do you check that an element is in a set?
Is there a simpler equivalent of the following code:
myset.find(x) != myset.end()
Source: (StackOverflow)
I'm trying to set the background color of a View (in this case a Button).
I use this code:
// set the background to green
v.setBackgroundColor(0x0000FF00 );
v.invalidate();
It causes the Button to disappear from the screen. What am I doing wrong, and what is the correct way to change the background color on any View?
Thanks.
Source: (StackOverflow)