## 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.

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

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

Comment hidden because of low score. Click to expand.
0

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

Comment hidden because of low score. Click to expand.
0

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

Comment hidden because of low score. Click to expand.
0

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

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.

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.

Comment hidden because of low score. Click to expand.
0

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)

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.

Comment hidden because of low score. Click to expand.
0

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).

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.

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.

Comment hidden because of low score. Click to expand.
0

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 .. :)

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

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

Comment hidden because of low score. Click to expand.
0

yup agree .... :)

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;

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);
}
}

{
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.PrintMessage();
}
}
}``````

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;
}
}

}
}``````

Comment hidden because of low score. Click to expand.
0

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;
}

Comment hidden because of low score. Click to expand.
0

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.

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(" ");
}

}
}

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("");
}
}

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,,,,,,

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

WHAT IS THE FUCKING QUESTION?

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

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

Comment hidden because of low score. Click to expand.
0

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

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

true saying boss

Comment hidden because of low score. Click to expand.

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.

### 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.