Google Interview Question
1,069 Interview Reviews |
Back to all Google Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Engineer at Google:
Write a program the generates the power set of a set of numbers
| Tags: | combinatorics, discrete, powerset See more , See less 8 |
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (8)
1 of 1 people found this helpful
What i did was, assuming the set has n elements:
for (i; from 0; to (2^n)-1)
{
generate binary represenation of i padded to n bits. (e.g if n=8, and i =4, representation would be 00100000). Call this representation "bitmap"
for (j; from 0; to bitmap.length)
{
if bitmap[j]==1: add set[j] to a tempset
}
add tempset to a global array of sets
}
return that global array of sets
Note that this has exponential complexity which i believe (but am not sure) is better than factorial complexity
Helpful Answer?
Yes |
No
Inappropriate?
O(n*2^n) is better than O(n!) as n grows.
Thanks for posting the question.
Helpful Answer?
Yes |
No
Inappropriate?
Set powerset(Set set) {
Set power = new HashSet();
Object[] a = set.toArray();
int n = a.length;
int maxCount = (int)Math.pow(2,n);
for (int i = 0; i < maxCount; i++){
Set subset = new HashSet();
char[] c = Integer.toBinaryString(i).toCharArray();
int m = c.length;
for (int j = m-1; j >= 0; j--){
if (c[j] == '1') {
subset.add(a[(n-1)-(m-1-j)]);
}
}
power.add(subset);
}
return power;
}
Helpful Answer?
Yes |
No
Inappropriate?
public List<List<Integer>> powerset(final List<Integer> set) {
// If set is empty, just return an empty powerset.
if(set.isEmpty()) return new ArrayList<List<Integer>>();
// Pop the first element as a candidate for composition.
final Integer i = set.remove(0);
// Get powerset of the set without the candidate.
final List<List<Integer>> subset = this.powerset(set);
// If the returned powerset is empty, add an empty set to it.
if(subset.isEmpty()) {
subset.add(new ArrayList<Integer>());
}
// For all the existing sets in the powerset without the candidate,
// add the all combination sets of existing sets plus the candidate
// to the powerset.
final int length = subset.size();
for(int j = 0; j < length; j++) {
final List<Integer> s = subset.get(j);
final List<Integer> sWithi = new ArrayList<Integer>(s);
sWithi.add(i);
subset.add(sWithi);
}
return subset;
}
Helpful Answer?
Yes |
No
Inappropriate?
public Set getPowerset(Set set) {
Set powerset = new HashSet();
powerset.add(new HashSet()); // Add the empty set
getPset(set.toArray(), new HashSet(), powerset, set.size(), 0);
return powerset;
}
private void getPset(Object[] input, Set subset, Set powerset, int n, int start) {
for (int i = start; i < n; i++) {
Object o = input[i];
subset.add(o);
powerset.add(new HashSet(subset));
if ( i < n-1) {
getPset(input, subset, powerset, n, i+1);
}
subset.remove(o);
}
}
Time for n = 16 for binary version: 10089 ms. Items: 65536.
Time for n = 16 for recursive version: 9379 ms. Items: 65536
Helpful Answer?
Yes |
No
Inappropriate?
1 of 1 people found this helpful
Good luck!
Helpful Answer?
Yes |
No
Inappropriate?
A little code in Python
def powerset(iterable):
from itertools import combinations
s = list(iterable)
return [ combinations(s, r) for r in range(len(s)+1) ]
if you can't import the combinations function, suggest you'll implement one yourself. Give a reasonable explanation of how you would do it.
Helpful Answer?
Yes |
No
Inappropriate?
Members can
answer or comment on this question
–
Join Now (It's Free) or
Sign In
1 of 1 people found this helpful
by ellemeno:
(Java)
/**
* The power set of S is the set of all subsets of S, including the empty
* set and S itself. It is parallel to the idea of all combinations of
* characters in a string.
*/
public Set<Set<Object>> getPowerSet(Set<Object> s){
Set<Set<Object>> pset = new HashSet<Set<Object>>();
Object[] a = s.toArray();
// Add the empty set
Set<Object> subset = new HashSet<Object>();
pset.add(new HashSet<Object>(subset));
int n = s.size();
this.getPset(a,subset,pset,n,0);
return pset;
}
private void getPset(Object[] a, Set<Object> subset, Set<Set<Object>> pset, int originalSize, int currentSize){
for (int i = currentSize; i < originalSize; i++){
subset.add(a[i]);
System.out.println("Attempting to add " + subset + ". i = " + i);
pset.add(new HashSet<Object>(subset));
if (i < originalSize - 1){
getPset(a,subset,pset,originalSize,i+1);
}
subset = new HashSet<Object>();
}
}