Yahoo Interview Question for Software Engineer / Developers






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

1st queen-4th row,1st column
2nd quenn-6th row,2nd column
3rd quenn-3rd row,3rd column
4th quenn-5th row,4th column
5th quenn-7th row,5th column
6th quenn-2nd row,6th column
7th quenn-8th row,7th column
8th quenn-1sr row,8th column

- gullu September 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

2nd row sixth and 6th row 2nd are each others attack position...

- TheDeathlyHollows July 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

http://en.wikipedia.org/wiki/Eight_queens_puzzle

- Hash October 19, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

durangobill.com/N_Queens.html

- sharath.ravindran March 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

easy

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

1st queen-4th row,1st column
2nd quenn-6th row,2nd column
3rd quenn-3rd row,3rd column
4th quenn-5th row,4th column
5th quenn-7th row,5th column
6th quenn-2nd row,6th column
7th quenn-8th row,7th column
8th quenn-1sr row,8th column

- gullu September 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Place one queen. Place another so they can't beat each other and recursively go on like this. Hard part is to figure out how to optimize the naive approach so you don't enumerate solutions twice and also don't do unnecessary work in general...

This is just a bit over the top for an interview but I am quite happy with it:

#include "stdafx.h"
#include <stdint.h>
#include <vector>
#include <unordered_set>
#include <iostream>
#include <future>

struct Position
{
	int32_t x, y;

	Position() : x(0), y(0) {}
	Position(int32_t x, int32_t y) : x(x), y(y) {}
};

template<class TCallback>
void asyncNextQueen(std::vector<std::future<void>>& tasks, std::vector<Position>& queens, const int N, int pass, const TCallback& callback)
{
	tasks.push_back(std::async([=, &tasks, &callback]()
	{
		std::vector<Position> copy = queens;
		nextQueen(tasks, copy, N, pass, callback);
	}));
}

template<class TCallback>
void nextQueen(std::vector<std::future<void>>& tasks, std::vector<Position>& queens, const int N, int pass, const TCallback& callback)
{
	if (queens.size() == N)
	{
		callback(queens);
		return;
	}

	for (int y = 0; y < N; y++)
	{
		Position pos = { pass, y };

		if (!canPlaceQueen(queens, pos))
			continue;

		queens.push_back(pos);

		if (pass == 1)
			asyncNextQueen(tasks, queens, N, pass + 1, callback);
		else
			nextQueen(tasks, queens, N, pass + 1, callback);

		queens.pop_back();
	}
}

bool canPlaceQueen(std::vector<Position>& queens, Position pos)
{
	for (auto q : queens)
	{
		if ((q.x == pos.x) || (q.y == pos.y) || (std::abs(q.x - pos.x) == std::abs(q.y - pos.y)))
			return false;
	}

	return true;
}

template<class TCallback>
void solveNQueens(const int N, const TCallback& callback)
{
	std::vector<Position> queens;
	std::vector<std::future<void>> tasks;
	queens.reserve(N);
	nextQueen(tasks, queens, N, 0, callback);

	for (auto& task : tasks) task.wait();
}

int main(int argc, char** argv)
{
	int count = 0;

	solveNQueens(16, [&](std::vector<Position>& queens)
	{
		count++;
	});

	std::cout << "DONE (" << count << ")!";
	std::cin.ignore();
	return 0;
}

- lunatic April 12, 2014 | 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