#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    10
    Rep Power
    0

    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
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    > 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?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    10
    Rep Power
    0
    No.. Im unable to answer that.. and hence posted it here to know the answers..
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    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

    • eramit2010 agrees : agree
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    10
    Rep Power
    0
    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

    • b49P23TIvg agrees : Now hiring.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    103
    Rep Power
    3
    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 = maxX-1; row < maxY; row++, cells++) {
    		System.out.print(matrix[row][col] + " ");
    	}
    	maxX--;
    	for (int row = maxY-1, col = maxX-1; col >= minX; col--, cells++) {
    		System.out.print(matrix[row][col] + " ");
    	}
    	maxY--;
    	for (int row = maxY-1, col = minX; row >= minY; row--, cells++) {
    		System.out.print(matrix[row][col] + " ");
    	}
    	minX++;
    }
    The way I approached this problem is as follows:
    1. I followed the advice above and wrote the algorithm on paper.
    2. I then proceeded to write code that worked only for a 3x3 matrix.
    3. 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.
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    India
    Posts
    95
    Rep Power
    4
    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.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    26
    Rep Power
    0
    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

    • jwdonahue disagrees : This a seperate problem that deserves another thread.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    10
    Rep Power
    0
    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 = maxX-1; row < maxY; row++, cells++) {
     		cout<<matrix[row][col]<<", "<<endl;
     	}
     	maxX--;
     	for (int row = maxY-1, col = maxX-1; col >= minX; col--, cells++) {
     		cout<<matrix[row][col]<<", "<<endl;
     	} 	maxY--;
     	for (int row = maxY-1, 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 ?
  18. #10
  19. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    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 2-D array, but initializing a 1-D array.

    Never ignore warnings! That alone should disqualify you from getting a job!
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    10
    Rep Power
    0
    Ya.. Changed the code for 2D arrays and updated in the post bro... Since I used online compiler I cant find any warnings.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    103
    Rep Power
    3
    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:
    1. I closely examined the correct input and output.
    2. I broke the problem down into smaller problems.
    3. I wrote code to display the first row of the matrix and tested it. Once it passed I moved onto the next problem.
    4. 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.
    5. 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.
    6. 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.
    7. I started by adding bounds for the first problem and tested it. Then I moved to the second, third, fourth, fifth, etc...
    8. Once I was satisfied with how everything worked I started testing the code with matrices of different sizes.
    9. Note: At the end of solving each problem I re-factored 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.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    May 2004
    Posts
    3,417
    Rep Power
    887
    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 hind-sight, 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.

IMN logo majestic logo threadwatch logo seochat tools logo