Interview Question for Software Developers


Country: India
Interview Type: Written Test




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

// n = 8 
	int calculateChessBoardSquares(int n) {
		int result = 0;
		for(int i=1; i<=n; i++) {
			result += i * i;
		}
		return result;
	}

       // formula for calculating sum: n^3/3 + n^2/2 + n/6
       int calculateChessBoardSquaresWithoutLoop(int n)    {
           return Math.Pow(n,3)/3 + Math.Pow(n,2)/2 + n/6
       }

- tus124 July 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

private static int count(int n) {
		if (n == 1) {
			return 1;
		}
		
		if (n == 0) {
			return 0;
		}
		
		return n * n + count(n -1); 
	}

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

C#:

void foo(int size){
int answer = 0;

for (int i = 1; i <= size; i++)
	answer += (i*i)

Console.Write(answer);

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

int squaresInaChess(int n)
{
   int sq = sqrt((double)n);
   int max = 0;
   int cur = 0;

   for (int i = 0; i < sq; i++)
   {
      for (int j = 0; j < sq; j++)
      {
         if (i == j)
            max = i + 1;


         if (max > (j+1))
         {
            cur = cur + (j + 1);
         }
         else
         {
            cur += max;
         }

      }
   }
   return cur;
}

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

Alternate Answer:
If the input is number of sides "n" in the board, answer would be:
- Iterate from n to 1
- Cumulative sum of squares of each would be number of squares.

For example: if n = 8,
number of squares = 64+49+36+25+16+9+4+1 = 204

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

private static int count(int n) {
		if (n == 1) {
			return 1;
		}
		
		if (n == 0) {
			return 0;
		}
		
		return n * n + count(n -1); 
	}

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

private static int count(int n) {
		if (n == 1) {
			return 1;
		}
		
		if (n == 0) {
			return 0;
		}
		
		return n * n + count(n -1); 
	}

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

private static int count(int n) {
		if (n == 1) {
			return 1;
		}
		
		if (n == 0) {
			return 0;
		}
		
		return n * n + count(n -1); 
	}

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

Well assuming that you are getting a matrix of colors for example you'll need to only care about the red and black colors.

public static int CalculateSquares(Color[][] board)
{
	Color prev = Color.White;

	int xCount = 0;
	for(int i = 0;i < board.Length; i++)
	{
		Color cur = board[i][0];
		if(cur != prev && (cur == Color.Black || cur == Color.Red))
		{
			xCount++;
		}
	}

	prev = Color.White;
	int yCount = 0;
	for(int i = 0;i < board[0].Length; i++)
	{
		Color cur = board[0][i];
		if(cur != prev && (cur == Color.Black || cur == Color.Red))
		{
			yCount++;
		}
	}

	return xCount*yCount;
}

- Nelson Perez July 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ChessBoard {
public static void main(String[] args){
int n=8;
int sum=0;
for(int i=1;i<=8;i++){
sum=sum+(n-i+1)*(n-i+1);
}
System.out.println("Number of squares in a chessboard are: "+sum);
}
}

- Guramrit42 July 27, 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