Capgemini Interview Question for Senior Software Development Engineers


Team: Chetu
Country: India
Interview Type: Written Test




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

A straight forward solution in O(m*n) time and O(1) space :

public void modifyArray(char[][] inputArray)
	{
		if(inputArray == null)
			return;
		int maxRow = inputArray.length-1;
		int maxColumn = inputArray[0].length-1;
		
		int iIndex1= 0;
		int jIndex1 = 0;
		
		int iIndex2= maxRow;
		int jIndex2 = maxColumn; 
		
		while(iIndex1<=iIndex2)
		{
			jIndex1 = 0;
			jIndex2 = maxColumn;
			while(jIndex1<=maxRow)
			{
				if(iIndex1 == iIndex2 && jIndex1 == jIndex2)
					return;
				swap(inputArray,iIndex1,jIndex1,iIndex2,jIndex2);
				jIndex1++;
				jIndex2--;
			}
			iIndex1++;
			iIndex2--;
		}
	}
	
	
	private void swap(char[][] inputArray, int iIndex1, int jIndex1,
			int iIndex2, int jIndex2) {
		char temp = inputArray[iIndex2][jIndex2];
		inputArray[iIndex2][jIndex2] = inputArray[iIndex1][jIndex1];
		inputArray[iIndex1][jIndex1] = temp;
	}

This solution would work for any (m*n) 2-d char array.

- teli.vaibhav October 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Apologies, I just realized the solution was expected in C#.. lol

- teli.vaibhav October 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The first mistake was assuming there was valid question in there. Forget the mistake about C#.

- Anonymous November 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Don't read too much into it dude. Just found something, figured the intention of the question and attempted it.

- teli.vaibhav November 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You didn't read the question too much, vaibhav. That is for sure :-)

- Anonymous November 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Keep pushing the element in stack while reading.
While writing pop from the stack and write it. After every 3rd element take a new line.

- Stupid Developer October 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Using a stack would use extra space. Why waste the space when it can be avoided and the problem can be in the same time.

- teli.vaibhav October 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

There will always be a trade-off between memory and time. The reason i suggested stack is because it looked straight forward and even the interviewer might wanted to test the knowledge of Datastructure. With stack time & memory both are O(n)

- Stupid Developer October 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

On second thoughts, it would be more impressive to come up with your stack solution first and then to point out there was a way the same time could be achieved inplace (O(1) space) and that if 'n' was huge you'd prefer the latter.

- teli.vaibhav October 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Are you sure you will go for latter if n would have been huge ??? Just check the time complexity. O(m*n) whereas memory would always be O(n).

- Stupid Developer October 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

I posted the code below. Its simple in-place swapping. It would save me O(m*n) space when either m or n are large.

- teli.vaibhav October 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Also, the stack solution gives you O(2*m*n). Although 2 is too small and is ignored for large values. You will ultimately end up spending more time.

- teli.vaibhav October 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

As per the assumption of this answer ....If we are sure that each line has size 3, than there will be no need to use extra space ....here is algo:

for(int i =0; i < 3; i++){
     for(int i =0; i < 3; i++){
            swap(a[i][j], a[2 - i][2 - j]);
     }
}

But i dont think that in real business scenarios we have such kind of privileges .. :)

- Ajeet October 29, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

@bossAjeet, bail this thread man.

This similar to each and everyone proving some different statement on the premise of a contradiction.

- Urik's twin Cookie Monster (dead now) October 29, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yup agree .... :)

- Ajeet October 29, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Akatsuki
{
    class Program
    {
        class MessageStack 
        {
            private Stack<char> charStack;

            public void PrintMessage()
            {
                char c;
                while (charStack.Count > 0)
                {
                    c = charStack.Pop();
                    Console.Write(c);
                }
            }

            public void AddMessage(string Message)
            {
                var x = Message.ToCharArray();
                charStack.Push('\n');
                for (int i = 0; i < x.Length; i++)
                {
                    charStack.Push(x[i]);
                }
            }

            public MessageStack()
            {
                charStack = new Stack<char>();
            }
        }
        static void Main(string[] args)
        {
            var msg = new MessageStack();
            msg.AddMessage("XWV");
            msg.AddMessage("UTS");
            msg.AddMessage("RQP");
            msg.PrintMessage();
        }
    }
}

- febsee October 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This looks like rotating a matrix 180. Here`s a version that rotates a NxN matrix in place:

static void rotateMatrix180(char[][] matrix) {

        int layers = matrix.length >> 1;
        int layer;
        for (layer = 0; layer < layers; ++layer) {
            for (int j = layer; j < matrix.length - layer; ++j) {

                char temp = matrix[layer][j];
                int oppRow = matrix.length - 1 - layer;
                int oppCol = matrix.length - 1 - j;
                matrix[layer][j] = matrix[oppRow][oppCol];
                matrix[oppRow][oppCol] = temp;

                if (j > layer && j < matrix.length - 1 - layer) {
                    temp = matrix[j][layer];
                    matrix[j][layer] = matrix[oppCol][oppRow];
                    matrix[oppCol][oppRow] = temp;
                }
            }

        }
    }

- pittbull October 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

May I ask what's the purpose for this:

if (j > layer && j < matrix.length - 1 - layer)
{
temp = matrix[j][layer];
matrix[j][layer] = matrix[oppCol][oppRow];
matrix[oppCol][oppRow] = temp;
}

- datacoder October 31, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The elements are swapped in place with only one temp variable. The matrix is swapped in frames where a frame is the most current outter first row ->last column ->last row -> last column. Since you start from the complete matrix and remove inverted frames one by one the size decreases by one.
Because in this case we do a 180 that if check that makes sure that when rotating the column we rotatae everything but the first and last elements in the column which were rotated with the row otherwise they'll be swapped back to their original positions.

- pittbull November 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class WarZone {
public static void main(String a[]){
String str = "XWV UTS RQP";
System.out.println(str);
String[] arr = str.split(" ");

for(int i=arr.length-1; i>=0; i--)
{

String[] arr1 = null;
for(int j=arr[i].length()-1; j>=0; j--) {

System.out.print(arr[i].charAt(j));
}
System.out.print(" ");
}

}
}

- Sam December 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void readArrayTranspose (char twoDArray [][]){
for(int i=twoDArray.length-1;i>=0;i--){
for(int j=twoDArray[i].length-1;j>=0;j--) {
System.out.print(twoDArray[i][j]);
}
System.out.println("");
}
}

- sharathdotc November 23, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I think ,THIS WORKS..ALGORITHM:

1)Do reverse of each row..like the following for this

i/p:

XWV
UTS
RQP

o/p:

VWX
STU
PQR

2)Do swap the first and last row only..keep the middle one as it as..

so the desired output is..


i/p:

VWX
STU
PQR

o/p:

PQR
STU
VWX i thnk ths is gonna works..wait fr code..i m thnking,,,,,,

- Anonymous October 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 5 vote

WHAT IS THE FUCKING QUESTION?

- Anonymous October 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

agree with the comment (not the swearing :P ).

- Urik's twin Cookie Monster (dead now) October 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

+1. Yes, stupid questions like these are annoying.

- Anonymous October 29, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

true saying boss

- Urik's twin Cookie Monster (dead now) October 29, 2013 | Flag
Comment hidden because of low score. Click to expand.


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