Zynga Interview Question for Developer Program Engineers






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

Here is my C++ style approach assuming that instead of printing the answers, you want to save them (i.e., the output result is a vector of strings).

int makeResultString(vector<string>& vzOutputItems, int k, int n)
{
	if (n < k)
	{
		return 1;
	}
	
	vzOutputItems.clear();
	int rflag = recursiveMakeResultString(vzOutputItems, k, n, 0, 0, "");
	return rflag;
}

int recursiveMakeResultString(vector<string>& vzOutputItems, 
								int k, 
								int n, 
								int currentPosition,
								int mostRecentNumMade,
								const string& zItemThusFar)
{
	for (int jj = mostRecentNumMade+1; jj <= n - (k-1) + currentPosition; ++jj)
	{
		char numAsChar = (char)(jj - '0');
		string zTempItem = zItemThusFar + numAsChar;
		if (currentPosition >= k-1)
		{
			vzOutputItems.push_back(zTempItem);
		}
		else
		{
			recursiveMakeResultString(vzOutputItems, 
										k, n, currentPosition+1, jj,
										zTempItem);
		}
	}
	return 0;
}

- Anonymous November 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

N choose K. Lookup Knuth for algorithms.

- Anonymous August 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        team(5,3);
    }
    
static void formTeam(int n,int k,int a[],int index,int i)
{
 if(index==k)
 {
  System.out.print("\n");
  for(int m=0;m<k;m++)
  System.out.print(a[m]+" ");
  return ;
 }
 for(int j=i;j<=n;j++)
 {
  a[index]=j;
  formTeam(n,k,a,index+1,j+1);
 }
}

static void team(int n,int k)
{
 int []a=new int[k];
 formTeam(n,k,a,0,1);
}

}

- techcoder October 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

totally incorrect!! how do you ensure b1<b2<b3
dumb

- abc November 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is the same solution as before, but with indentation corrected:

int makeResultString(vector<string>& vzOutputItems, int k, int n)
{
	if (n < k)
	{
		return 1;
	}
	
	vzOutputItems.clear();
	int rflag = recursiveMakeResultString(vzOutputItems, k, n, 0, 0, "");
	return rflag;
}

int recursiveMakeResultString(vector<string>& vzOutputItems, 
				int k, 
				int n, 
				int currentPosition,
				int mostRecentNumMade,
				const string& zItemThusFar)
{
	for (int jj = mostRecentNumMade+1; jj <= n - (k-1) + currentPosition; ++jj)
	{
		char numAsChar = (char)(jj - '0');
		string zTempItem = zItemThusFar + numAsChar;
		if (currentPosition >= k-1)
		{
			vzOutputItems.push_back(zTempItem);
		}
		else
		{
			recursiveMakeResultString(vzOutputItems, k, n, currentPosition+1, jj, zTempItem);
		}
	}
	return 0;
}

- Anonymous November 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class GenerateTeamName {

public static void main(String[] args) {
formTeam(1, 5, 3, "");
}

private static void formTeam(int c, int n, int k, String team) {
int currentLength = team.length();
if (currentLength == k) {
System.out.println(team);
return;
}
for (int idx = c; n-idx+1 >= k - currentLength;idx++) {
formTeam(idx+1, n, k, team + idx);
}
}

}

- jts November 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

I think theres an easy solution to this.
For eg: n=5, k=3
Possible numbers to use are : 1 2 3 4 5
For a k-element array, if array indices started from 1 at any index i numbers go from i to (n - k + i)

For C-style arrays its just i +1 to (n - k + i) +1

void insert (char* str, int i, int j)	{
	if( i > k-1 || j > n-k+i+1 )
		return;
	if( i < k-1)	{
		str[i] = '0' + j;
		insert(str, i+1, j+1);
		insert(str, i, j+1);
	}
	if( i == k)	{
		str[i] = '0' + j;
		printf(Team : %s \n",str);
		insert(str, i, j+1);
	}
}

In main on a call insert(0,1)
Output :
Team : 123
Team : 124
Team : 125
Team : 134
Team : 135
Team : 145
Team : 234
Team : 235
.
.
.
.

- o.O January 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think it should be:
if (i == k - 1) {
  ...
}

- Anonymous January 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

245 is missing?

- Rohan January 21, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Very simple solution

For i =0 i < n i++
for j = i + 1 i < n j ++
for k = j + 1 k < n k++
print vec[i] vec[j] vec[k]

Assumes the vector is sorted
Can be generalized to an arbitrary number of loops

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

This is my take in Python, please critique:

def form_team(n,k):
	a = [i for i in range(1,n-k+1)]
	print a
	a = form(a,n,k-1)
	return a

def form(a,n,k):
	new_list = []
	for i in a:
		for j in range(i%10+1,n-k+2):
			new_list.append(i*10+j)
	print new_list
	if ( 1 == k ):
		return new_list
	else:
		return form(new_list,n,k-1)

	
print form_team(100,5)

- Ethan March 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void teamName(int begin, int n, int k, vector<vector<int> > &name_seq, vector<int> &name)
{
if(n < k)
return;
if(!k)
{
name_seq.push_back(name);
return;
}

name.push_back(begin);
teamName(begin + 1, n - 1, k - 1, name_seq, name);
name.pop_back();
teamName(begin + 1, n - 1, k, name_seq, name);
}

vector<vector<int> > teamName(int n, int k)
{
vector<vector<int> > name_seq;
vector<int> name;
teamName(1, n, k, name_seq, name);
return name_seq;
}

- UNFounder January 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void teamMembers(int *&a,int k, int n, int i, int j)
{

if (j == k )
{
for (int m = 0; m < k; m++)
printf("%d", a[m]);
printf("\n");
return;
}
for (int m = i+1; m <= n; m++)
{
a[j] = m;
teamMembers(a, k, n, m, j + 1);
}

}

int main()
{
const int N = 4,K = 3;
int *array = new int[K];
for (int i = 0; i < K; i++)
array[i] = 0;
teamMembers(array, K, N, 0, 0);
return 0;
}

- Rohan January 21, 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