Interview Question


Country: United States




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

void generate(int n) {
		if (n < 1) {
			System.out.println(Arrays.toString(A));
			return;
		}
		A[n - 1] = 0;
		generate(n - 1);
		A[n - 1] = 1;
		generate(n - 1);
	}

- Anonymous October 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

this is kind of wrong..coz in the above code it goes wrong when n numbers are all zeroes..

- c7c7 October 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i did not run a test for codes above.

it is in java. i know nothing about java.

i have one in C, but i think it is so difficult to understand it:

void foo(int *arr,int n,int i)
{
    if (i==n) {
        for (i = 0; i < n; i++) {
            printf("%d\t",arr[i]);
        }
        printf("\n");
    }else{
        arr[i]=1;
        foo(arr,n,i+1);
        arr[i]=0;
        foo(arr,n,i+1);
    }

- nz2324nz2324 October 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

we have to permute the n numbers in such a way that there are no repetitions..

- c7c7 October 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

basically in this question we have suppose a ->zeroes and b->ones then we need to permute then such that,possible arrangements are n!/(a!*b!)..
so can anyone help me how to code that thing..as i want to print non-repeated permutations..i m unable to find its solution..

- c7c7 October 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

increment a n bit unsigned number until the value overflows back to 0
print the values alone the way

- EthOmus October 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The following code may avoid repetitions:

public static void printAll(int[] data)
    {
        if(data == null) return;
        if(data.length == 0) return;
        
        int numOne = 0;
        for(int i = 0; i < data.length; ++i)
        {
            numOne += data[i];
        }
        int numZero = data.length - numOne;
        String str = "";
        printHelper(numZero, numOne, 0, str, 0, 0);
    }
    
    private static void printHelper(int numZero, int numOne, int level, String str, int numZeroUsed, int numOneUsed)
    {
        if(level == numZero + numOne)
        {
            System.out.println(str);
            return;
        }
        if(numZeroUsed < numZero)
        {
            String strNew = str;
            strNew += "0";
            printHelper(numZero, numOne, level+1, strNew , numZeroUsed+1, numOneUsed);
        }
        if(numOneUsed < numOne)
        {
            String strNew = str;
            strNew += "1";
            printHelper(numZero, numOne, level+1, strNew, numZeroUsed, numOneUsed+1);
        }
    }

- Richard October 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. count the number of ones
2. put a 1 at the lowest index, recursively put the rest ones in the rest n - 1 positions
3. repeat step 2 for 2, 3, ...

- nixfish October 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I am unable to understand this algo..

can you give some codes?

- nz2324nz2324 October 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

here comes the codes in Python:

import itertools
m, n = 3, 2
for i in itertools.product(*((range(n), )*m)):
    print i

- nz2324nz2324 October 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#define N 3
main(x){
	int i;  
	for (i = 0; i<N;printf("%d",x >> i &1), i++); 
	printf("\n");
	(1 << N)-x && main(x+1); 
	return 0; 
}

or use this function:

void foo(unsigned int n)
{
	int i, j; 
	for (i = 0; i < 1 << n; printf("\n"), i++)
		for (j = 0; j < n; printf("%d", i >> j&1), j++); 
}

- nz2324nz2324 October 28, 2012 | 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