Interview Question


Country: United States




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

public class PowetSet {
public static void main(String[] args) {
char set[] = { 'a', 'b', 'c' };
printPowerSet(set, 3);
}

static void printPowerSet(char set[], int set_size) {
/*
* set_size of power set of a set with set_size n is (2**n -1)
*/
int pow_set_size = (int) Math.pow(2, set_size);
int counter, j;

/* Run from counter 000..0 to 111..1 */
for (counter = 0; counter < pow_set_size; counter++) {
for (j = 0; j < set_size; j++) {
/*
* Check if jth bit in the counter is set If set then pront jth
* element from set
*/
int value = 1 << j;
// System.out.println("value" + value);
int res = counter & (value);
//System.out.println("res ::" + res);
// Take any of the below if condition.. In both the case it'll work
//if (res != 0)
if (res == 0)
System.out.print(
// "set[j]::" +
set[j]
// + "\t" + ":: j :: "
// + j + "\t"
);
}
System.out.print("\t");
}
}
}

- UniqueT July 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class PowetSet {
	public static void main(String[] args) {
		char set[] = { 'a', 'b', 'c' };
		printPowerSet(set, 3);
	}

	static void printPowerSet(char set[], int set_size) {
		/*
		 * set_size of power set of a set with set_size n is (2**n -1)
		 */
		int pow_set_size = (int) Math.pow(2, set_size);
		int counter, j;

		/* Run from counter 000..0 to 111..1 */
		for (counter = 0; counter < pow_set_size; counter++) {
			for (j = 0; j < set_size; j++) {
				/*
				 * Check if jth bit in the counter is set If set then pront jth
				 * element from set
				 */
				int value = 1 << j;
				// System.out.println("value" + value);
				int res = counter & (value);
				//System.out.println("res ::" + res);
				// Take any of the below if condition.. In both the case it'll work
				//if (res != 0)
				if (res == 0)
					System.out.print(
					// "set[j]::" +
							set[j]
							// + "\t" + ":: j :: "
							// + j + "\t"
							);
			}
			System.out.print("\t");
		}
	}
}

- UniqueT July 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class PowerSet{
	public static void main(String[] args) {
		PowerSet main = new PowerSet();
		main.printPowerSet("ABCDE");
	}
	
	//print powerset
	public void printPowerSet(String set){
		int length=set.length();
		for(int i=0;i<length;i++){
			System.out.println(set.substring(i, i+1));
			for(int j=i+1;j<length;j++){
				for(int k=j;k<length;k++){
					System.out.println(set.substring(i, j)+set.substring(k,k+1));
				}
			}
		}
	}

}

- P Siva SN August 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class PowerSet {

	public static void main(String[] args) {
		PowerSet main = new PowerSet ();
		
		main.printPowerSet("ABC");
		
	}
	
	
	
	//print powerset
	public void printPowerSet(String set){
		int length=set.length();
		for(int i=0;i<length;i++){
			System.out.println(set.substring(i, i+1));
			for(int j=i+1;j<length;j++){
				for(int k=j;k<length;k++){
					System.out.println(set.substring(i, j)+set.substring(k,k+1));
				}
			}
		}
	}
}

- P Siva SN August 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
{{{ public class PowerSet { public static void main(String[] args) { PowerSet main = new PowerSet (); main.printPowerSet("ABC"); } //print powerset public void printPowerSet(String set){ int length=set.length(); for(int i=0;i<length;i++){ System.out.println(set.substring(i, i+1)); for(int j=i+1;j<length;j++){ for(int k=j;k<length;k++){ System.out.println(set.substring(i, j)+set.substring(k,k+1)); } } } } } }} - imsiva7 August 14, 2015 | 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