Google Interview Question for Software Engineer in Tests


Country: United States




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

#include <iostream>
using namespace std;

int m[30],s=1,nq=10;
int checker(int k,int i)
{
	
	int j;
     for(j=1;j<=k-1;j++)
     {
      if((m[j]==i)||(m[j]-i==j-k) || (m[j]-i==k-j))
      {
       return(0);
      }
     }
     return(1);
}

void solve(int n)
{
	if(n==nq+1)
	{
		
		cout<<endl<<endl<<endl<<"soln"<<s++;
		for(int i=1;i<n;i++)
		{
			cout<<endl;
			for(int j=1;j<n;j++)
			{
				if( (m[i]==j))
				cout<<"Q";
				else
				cout<<"-";
			}
		}
		
	
	}
	else
	{
		for(int i=1;i<=nq;i++)
		{
			if(checker(n,i)==1)
			{
				m[n]=i;
				solve(n+1);
			}
		}
		
	}
}

			
	
	
	

int main()
{
	solve(1);
}

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

If the n is 3, then there will be no solution. When do you plan to stop?

Can you provide me with an algorithm that you are using in plain English rather than code?

One important thing to use is backtracking and figured that out.

- Dev March 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hey Thanks for your algo.

Can you please give me explanation of if condition in checker function?

Thanks.

- Vinay April 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

//modified for generalized case
int *m;

int checker(int k,int i){
	int j;

	for(j=1;j<=k-1;j++) 
    {
		if((m[j]==i)||(m[j]-i==j-k) || (m[j]-i==k-j)) 
		{
			return(0);
		}
	}
	return(1);
}

void solve(int n, int nq){
	if(n==nq+1)	{
		cout<<endl<<endl<<endl<<"soln"<<s++;
		for(int i=1;i<n;i++)
		{
			cout<<endl;
			for(int j=1;j<n;j++) 
			{
				if( (m[i]==j))
					cout<<"Q";
				else
					cout<<"-";
			}
		}
	}
	else
	{
		for(int i=1;i<=nq;i++)
		{
			if(checker(n,i)==1)
			{
				m[n]=i;
				solve(n+1);
			}
		}
	}
}



int main()
{
	int nq;
	cout<<"Enter number of queens:\n";
	cin>>nq;
	if (nq<=0)
	{
		cout<<"enter valid value of number of queens";
		return 1;
	}
	m = new int[nq+1];
	solve(0,nq);
}

- googlybhai September 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

//Making multi-threadedsafe



int checker(int k,int i){
	int j;

	for(j=1;j<=k-1;j++) 
    {
		if((m[j]==i)||(m[j]-i==j-k) || (m[j]-i==k-j)) 
		{
			return(0);
		}
	}
	return(1);
}

void solve(int n, int nq, int *m){
	if(n==nq+1)	{
		cout<<endl<<endl<<endl<<"soln"<<s++;
		for(int i=1;i<n;i++)
		{
			cout<<endl;
			for(int j=1;j<n;j++) 
			{
				if( (m[i]==j))
					cout<<"Q";
				else
					cout<<"-";
			}
		}
	}
	else
	{
		for(int i=1;i<=nq;i++)
		{
			if(checker(n,i)==1)
			{
				m[n]=i;
				solve(n+1);
			}
		}
	}
}



int main()
{
	int nq;
                int *m;
	cout<<"Enter number of queens:\n";
	cin>>nq;
	if (nq<=0)
	{
		cout<<"enter valid value of number of queens";
		return 1;
	}
	m = new int[nq+1];
	solve(1,nq, m);
}

- googlybhai September 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int *queens(int *list,int *used,int level,int Ispossible)
{
if (level==N)
{ printf("the result is %s",list);
Ispossible=1; // find possible solution, set the mask
}
else
for(int i=0;i<N;i++) // variable i stands for each column
{
Is_diag=0;
if (used[i]==1) // used[] stores the already taken row number for previous column
continue;
for (int j=0;j<level;j++)
if (level-j==abs(i-list[j])) // check the diagonal and reversed diagonal
Is_diag=1;
if (Is_diag)
continue;
else {
list[level]=i; // store the row number for the level column
used[i]=1;
queue(N,list,used,level+1); // jump to the next recursion
used[i]=0;
}
}
}

void findsolution(int N)
{
int *list=(int *)malloc(sizeof(int)*N);
int level=0,Ispossible=0;
int *used(int *)malloc(sizeof(int)*N);
queens(list,used,level,Ispossible);
free(list); free(used);
if (!Ispossible)
printf("No Solution");
}

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

Here is an iterative solution using some trigonometry

<?php

$check = NQueen(8);

function NQueen($n) {
	$positions = array();
	for ($index = 0; $index < $n; $index++) {
		$positions[] = -1; 
	}
	$curColumn = 0;
	while (true) {
		$posFound = false;
		for ($i = $positions[$curColumn] + 1; $i < $n; $i++) {
			if (isSafe($curColumn, $i, $positions, $n)) {
				$posFound = true;
				$positions[$curColumn] = $i;
				break;
			}
		}
		if ($posFound) {
			if ($curColumn >= $n - 1) {
				return true;
			}
			$curColumn++;
		} else {
			if ($curColumn <= 0) {
				return false;
			}
			if ($positions[$curColumn] >= 0) {
				$positions[$curColumn] = -1;
			}
			$curColumn--;
			if ($positions[$curColumn] == ($n -1)) {
				$positions[$curColumn] = -1;
				$curColumn--;
			}
		}
	}
}

function isSafe($curColumn, $row, $positions, $n) {
	$x2 = $curColumn;
	$y2 = $row;
	for ($index = 0; $index < $curColumn; $index++) {
		$x1 = $index;
		$y1 = $positions[$index];
		$m = abs(($y2 - $y1) / ($x2 - $x1));
		if ($m == 1 || $m == 0) {
			return false;
		}
	}
	return true;
}

- Sem March 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

Use simple recursion !

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

backtracking!!!

- codinglearner March 13, 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