Google Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
1
of 1 vote

For a set of size n, generate all binary numbers from 0 to 2^n-1

suppose the set is {'a','b','c'} (size =3)

Binary numbers from 0 to 7 are
000
001
010
011
100
101
110
111

For any binary number, say 101, look at the position of 1 and replace that from the set
ie for 101 = ac
for 011 = bc
111 = abc
000 = {}
010 = b

and so on..

- Binary June 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;

namespace Testing
{
public class PowerSet
{
public static void GeneratePowerSet(int[] array, int index, string str)
{
if (index >= array.Length)
{
Console.WriteLine(str.Trim());
}
else
{
GeneratePowerSet(array, ++index, str);
if (index < array.Length)
{
str = str + " " + array[index];
GeneratePowerSet(array, index, str);
}
}
}
}

public sealed class MAIN
{
public static void Main()
{
int[] array = new int[] { 1,2,3,4 };
PowerSet.GeneratePowerSet(array, -1, string.Empty);
Console.ReadLine();
}
}
}

- champaklal June 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@champakla, would you plz explain the idea a little?
I'm always poor too catch up such code where there is tail-recursion (i.e. recursive function calls in the tail part of initial recursion call) :(

Thanks.

- anonymous June 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

the idea goes like this:

a subset in a powerset can contain an element or it can not. so for every element, we have two choices.

Suppose the first element is taken, or not taken, this would form 2 subsets.
similarly, second element can be taken or not taken, and these 2 possibilities can be hooked to the previous two, generating 4 solutions...
so on and so forth. Now, as soon as we have accounted for presence and absence of all the elements one by one, we can print their respective subsets. the subsets so formed are 2^n for n elements. that's why they are called powersets, as they are exponential.
for more details, contact me on priyodit[AT]gmail [DOT] com as i don't visit here often.

- champaklal June 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

idea is to generate all numbers from 0 to 2^length_of_set... java code goes below...

public class PowerSet {
	public static void main(String[] args) {
		int[] a = {1, 2, 3, 10, 11};
		printPowerSet(a);
	}
	
	public static void printPowerSet(int[] a){
		int number = (int)Math.pow(2, a.length);
		
		System.out.println("1: {}");
		for(int i=1; i<number; i++){
			System.out.print((i+1) + " : {");
			for(int j=0; j< a.length; j++){
				if(((i >> j) & 1) == 1){
					System.out.print(a[j] + "  ");
				}
			}
			System.out.println("}" );
		}
	}
}

- binary? June 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Same Idea what binary gave us

public class PowerSet {
	public static void main(String args[]){
		char[] set = {'a','b','c'};
		int max = ~(~0<<(set.length));
		for(int i=0;i<=max;i++){
			int k=set.length;
			System.out.print("{");
			for(int j=0;j<k;j++){
				if((i&(1<<j))>0){
					System.out.print(set[j]);
				}
			}
			System.out.print("}");
		}
	}
}

- MaYanK June 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="java" line="1" title="CodeMonkey82374" class="run-this">/* The class name doesn't have to be Main, as long as the class is not public. */
class PowerSet
{
public static void main(String[] args) {

char[] set = {'a','b','c'};

int max = 1 << set.length;

for(int i=1; i<=max; i++){

int k = set.length;

System.out.print("{");

for(int j=0; j<k; j++){

if( (i & (1<<j)) != 0){
System.out.print(set[j]);
}
}

System.out.print("}");

}
}
}

</pre><pre title="CodeMonkey82374" input="yes">

</pre>

- Anonymous June 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can we not also do this by using recursion & dynamic programming ?

- dynamic programming / Recursion June 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

time complexity of the above algorithm is
O (n * 2^n)

- Time Complexity June 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def power_set(S):
  if not S:
    return []
  T = power_set(S[1:])
  return T + [[S[0]] + t for t in T]

print power_set(S)

- Bullocks September 06, 2010 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More