July 22nd, 2013, 09:36 AM

I need answer for a matrix program asked in an interview
I attended an interview today.
I was given a question there...
A matrix should have the spiral order of output.
i.e
Write a program to generate the given output using the given input
Input:
1 2 3
4 5 6
7 8 9
Output:
123698745
July 22nd, 2013, 01:19 PM

> I attended an interview today.
Congratulations.
> I was given a question there...
That's pretty common at interviews, to be given questions to answer.
> Write a program to generate the given output using the given input
Cool  did you manage to make it then?
July 22nd, 2013, 01:38 PM

No.. Im unable to answer that.. and hence posted it here to know the answers..
July 22nd, 2013, 02:08 PM

So have you made any attempt at solving it?
What do you get if you get the right answer  another crack at the job interview? A job?
Here's a hint
Right 3
Down 2
Left 2
Up 1
Right 1
Try it with a larger matrix, and figure out the pattern.
When you've done that, then you can start to think about writing code.
And no, this isn't the kind of place where you can roll in with a question (and no effort), and expect an answer on a plate. We expect YOU to make an effort, and demonstrate that you're learning something along the way.
Comments on this post
July 22nd, 2013, 08:48 PM

Already I found this one.
Right 3  j++
Right 2  i++
Right 2  j
right 1  i
right 1  j++
similarly for 4 x 4 matrix,
Right 4  j++
Right 3  i++
Right 3  j
right 2  i
right 2  j++
right 1  i++
right 1  j
Bt I dono how to proceed with this data...
Comments on this post
July 22nd, 2013, 11:15 PM

This was a pretty fun problem. Took me about 20 minutes to get it. I wrote it in Java. My solution probably isn't the best, but here it is. I tested it on a 4x3 and a 3x3. Sorry for posting a direct solution. I had so much fun writing it that I just wanted to share it :(
Code:
int minX = 0, maxX = width;
int minY = 0, maxY = height;
for (int cells = 0; cells < height * width;) {
for (int row = minY, col = minX; col < maxX; col++, cells++) {
System.out.print(matrix[row][col] + " ");
}
minY++;
for (int row = minY, col = maxX1; row < maxY; row++, cells++) {
System.out.print(matrix[row][col] + " ");
}
maxX;
for (int row = maxY1, col = maxX1; col >= minX; col, cells++) {
System.out.print(matrix[row][col] + " ");
}
maxY;
for (int row = maxY1, col = minX; row >= minY; row, cells++) {
System.out.print(matrix[row][col] + " ");
}
minX++;
}
The way I approached this problem is as follows:
 I followed the advice above and wrote the algorithm on paper.
 I then proceeded to write code that worked only for a 3x3 matrix.
 I adapted that code using bounds (maxX, maxY, etc) to make it work on any size.
Last edited by Cameron0960; July 22nd, 2013 at 11:20 PM.
July 23rd, 2013, 02:42 AM

Originally Posted by csetube
Already I found this one.
Right 3  j++
Right 2  i++
Right 2  j
right 1  i
right 1  j++
similarly for 4 x 4 matrix,
Right 4  j++
Right 3  i++
Right 3  j
right 2  i
right 2  j++
right 1  i++
right 1  j
Bt I dono how to proceed with this data...
you should get logic from it.
its start from size of matrix. eg 4
then 4 33 22 11
and for increment and decrements once increment then decrements.
try prog. by urself.
July 23rd, 2013, 12:38 PM

hi sir i am yashwamnth and i always wanted to become a software wizard.but i m just a school student,and i do programming as a hobby.i like to know about the methods of solving a problem.no proper teacher for me in the school so i fetch your help.can you help me.in a book i saw a question to printout the adam number between 10 and 100.i dont want the code but i need some logic about the problem.the thing i cant figure out is that how to reverse the given number.thanks in advance
Comments on this post
July 23rd, 2013, 01:56 PM

This works great bro...
Thanks @Cameron0960.
This is the full C++ code which I compiled online.
Works fine for all array sizes..
Thank u so much:)
Code:
#include <iostream>
using namespace std;
int main()
{
int width=5, height=5;
int matrix[5][5] = {{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15},{16,17,18,19,20},{21,22,23,24,25}};
cout << "Hello World" << endl;
int minX = 0, maxX = width; int minY = 0, maxY = height;
for (int cells = 0; cells < height * width;) {
for (int row = minY, col = minX; col < maxX; col++, cells++) {
cout<<matrix[row][col]<<", "<<endl;
}
minY++;
for (int row = minY, col = maxX1; row < maxY; row++, cells++) {
cout<<matrix[row][col]<<", "<<endl;
}
maxX;
for (int row = maxY1, col = maxX1; col >= minX; col, cells++) {
cout<<matrix[row][col]<<", "<<endl;
} maxY;
for (int row = maxY1, col = minX; row >= minY; row, cells++) {
cout<<matrix[row][col]<<", "<<endl;
}
minX++;
}
return 0;
}
I got some ideas and Im unable to code for them as u...
So, can u guide me how to code our thinking ?
July 23rd, 2013, 02:49 PM

No, it does not work! You are getting warnings!
C:TEST>g++ Wall csetube.cpp
csetube.cpp: In function `int main()':
csetube.cpp:6: warning: aggregate has a partly bracketed initializer
csetube.cpp:6: warning: aggregate has a partly bracketed initializer
csetube.cpp:6: warning: aggregate has a partly bracketed initializer
csetube.cpp:6: warning: aggregate has a partly bracketed initializer
csetube.cpp:6: warning: aggregate has a partly bracketed initializer
C:TEST>
This is wrong:
int matrix[5][5] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25};
HINT: you are declaring a 2D array, but initializing a 1D array.
Never ignore warnings! That alone should disqualify you from getting a job!
July 23rd, 2013, 10:12 PM

Ya.. Changed the code for 2D arrays and updated in the post bro... Since I used online compiler I cant find any warnings.
July 24th, 2013, 01:49 AM

Originally Posted by csetube
This works great bro...
Thanks @Cameron0960.
This is the full C++ code which I compiled online.
Works fine for all array sizes..
Thank u so much:)
I got some ideas and Im unable to code for them as u...
So, can u guide me how to code our thinking ?
Your welcome. I took the same approach I use to solve any problem. The steps I took to solve this problem are as follows:
 I closely examined the correct input and output.
 I broke the problem down into smaller problems.
 I wrote code to display the first row of the matrix and tested it. Once it passed I moved onto the next problem.
 The second problem was displaying the far right column. The third problem was displaying the bottom row. The fourth was displaying the first column. The fifth was displaying the middle value.
 This didn't give me a complete solution, but it did give me the basis for one. From going this far I saw that there were repeated values that I needed to eliminate.
 I thought up a couple ways to remove the repeated values. I decided that if I could change where I start and end my iterations, then that would fix the problem.
 I started by adding bounds for the first problem and tested it. Then I moved to the second, third, fourth, fifth, etc...
 Once I was satisfied with how everything worked I started testing the code with matrices of different sizes.
 Note: At the end of solving each problem I refactored my code to ensure that it was readable and clear.
That's how I came about my solution. Not an easy question for an interview IMO.
Last edited by Cameron0960; July 24th, 2013 at 01:52 AM.
July 24th, 2013, 10:54 PM

Whether you solve an interview problem, or not, is usually less important than how you go about failing or succeeding at it. This is a trivial problem for most of us and you can assume that is always the case for the interviewer. The key for you is to actually understand the problem and convey to the interviewer that that is the case. That involves you restating the problem in your own words; and, as is usually the case with trivial interview problems, "discovering the obvious", or at least putting forth some systematic effort at doing so.
A 2D matrix is equivalent to a 2D plane of coordinates, each with an assigned property (the value at that cell). The problem description involves navigating through all of the points, once and only once, by way of spiralling into the center. The first thing that always comes to my mind on these problems is the description of the path; and the best way to describe that path is by way of the coordinates. A simple imaginary walk along the path as you state the X/Y coordinates of each cell would have revealed the essential algorithm to you and even if you did not then write that code, the interviewer would have seen you working the problem algorithmically.
If you admit defeat on one these problem before convincing the interviewer that you made a good honest attempt, it is unlikely you will be hired if you do not somehow redeem yourself before the interview is over. Most interview problems are really quite trivial in hindsight, but interviewers exploit the fact that they are not all well known. Either you demonstrate experience by having already known of the solution or you demonstrate that you know how to solve problems you haven't seen before.
Solving new problems is a basic skill you were born with and should still be able to demonstrate prior to graduating from elementary school. It is unfortunate that so many education systems on this planet actually expend so much energy "making you smart" by wiping out your innate skills for experimentation and discovery. It's a sign of the times that you did not initially search for the method rather than the answer.
Apply the scientific method in your next interview and it probably won't matter if you ever succeeded at finding the solution in the time span of an interview.
I no longer wish to be associated with this site.