Groupon Interview Question for Senior Software Development Engineers


Country: India
Interview Type: Phone Interview




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

Print up, then right, then down, then left, till up > down or left > right. Following is code in C.

void sprialPrint(int** matrix, int row, int col)
{
   int i, up = 0, down = row-1, left = 0, right = col-1;

   while(1){
       if(left > right) break;
       for(i = left; i <= right; ++i) printf("%d ", matrix[up][i]);
       ++up;
       
       if(up > down) break;
       for(i = up; i <= down; ++i) printf("%d ", matrix[i][right]);
       --right;

       if(left > right) break;
       for(i = right; i >= left; --i) printf("%d ", matrix[down][i]);
       --down;

       if(up > down) break;
       for(i = down; i >= up; --i) printf("%d ", matrix[i][left]);
       ++left;
   }
}

- uuuouou January 13, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is my program in Java:

/*
 * This program is written to print the spiral of a given rectangular matrix in clockwise manner.
 * This is done by maintaining two pointer for rows and two pointers for columns starting from top to bottom for rows and left to right for columns.
 * The Big-Oh of this program is O(n ^ 2)
 * 
 * @author Prabhash Rathore
 * @date Jan 25, 2014
 * 
 */

public class SpiralMatrix {

	private static final char[][] a = {{'1', '2', '3', '4', '5'}, {'6', '8', '9', 'a', 'b'}, {'c', 'd', 'e', 'f', 'g'}, {'h', 'i', 'j', 'k', 'l'}};
	
	public static void printSpriralMatrix(char[][] matrix, int rowLength, int colLength) {
		
		int topRowPointer = 0; // top pointer to row position
		int leftColPointer = 0; // left pointer to column position
		
		int bottomRowPointer = rowLength - 1; // bottom pointer to row position 
		int rightColPointer = colLength - 1; // right pointer to column position
		
		System.out.println("Here is the Spiral Matrix: ");
		
		while((topRowPointer < rowLength/2) && (leftColPointer < colLength/2)) {
			
			// loop variables
			int i = 0;
			int j = 0;
			
			for(i = topRowPointer, j = leftColPointer; j < rightColPointer; j++)
				System.out.println(matrix[i][j]);
			
			for(i = topRowPointer, j = rightColPointer; i < bottomRowPointer; i++)
				System.out.println(matrix[i][j]);
			
			for(i = bottomRowPointer, j = rightColPointer; j > leftColPointer; j--)
				System.out.println(matrix[i][j]);
			
			for(i = bottomRowPointer, j = leftColPointer; i > topRowPointer; i--)
				System.out.println(matrix[i][j]);
			
			
			topRowPointer++;
			leftColPointer++;
			bottomRowPointer--;
			rightColPointer--;			
			
		}
		
	}
	
	public static void main(String[] args) {
		
		int rowSize = a.length;
		System.out.println("Row size is: " + rowSize);
		
		int colSize = a[0].length; //assuming given matrix is a rectangular matrix
		System.out.println("Column size is: " + colSize);
		
		// print the input matrix
		System.out.println("Here are the elements in 2D Matrix:");
		for(int i = 0; i < rowSize; i++) {
			for(int j = 0; j < colSize; j++)
				System.out.println(a[i][j]);
		}
		
		printSpriralMatrix(a, rowSize, colSize);

	}

}

- prabhashrathore January 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Everything is perfect except one thing..instead of println, if print is used to display in the function, output looks in a straight line. :)

- Hari March 22, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

class SpiralMove2DArray
    {

        int[,] array;
        int[,] traversed;

        const int ArrayBound = 4;

        enum direction
        {
            east, south, west, north
        }

        direction MoveDir;

        int xIndex, yIndex;

        public SpiralMove2DArray()
        {

            array = new int[ArrayBound, ArrayBound];
            traversed = new int[ArrayBound, ArrayBound];

            MoveDir = direction.east;
            xIndex = 0;
            yIndex = 0;

            int k = 0;
            for (int i = 0; i < ArrayBound; i++)
            {
                for (int j = 0; j < ArrayBound; j++)
                {
                    array[i, j] = k;
                    k++;

                }
            }

            PrintValue();

            while (CanTraverse())
            {
                switch (MoveDir)
                {
                    case direction.south:
                        if (CanMoveSouth())
                        {
                            xIndex += 1;
                            PrintValue();
                        }
                        else
                        {
                            MoveDir = direction.west;
                        }
                        break;

                    case direction.east:
                        if (CanMoveEast())
                        {
                            yIndex += 1;
                            PrintValue();
                        }
                        else
                        {
                            MoveDir = direction.south;
                        }

                        break;

                    case direction.north:
                        if (CanMoveNorth())
                        {
                            xIndex -= 1;
                            PrintValue();
                        }
                        else
                        {
                            MoveDir = direction.east;
                        }

                        break;

                    case direction.west:
                        if (CanMoveWest())
                        {
                            yIndex -= 1;
                            PrintValue();
                        }
                        else
                        {
                            MoveDir = direction.north;
                        }

                        break;

                    default:
                        break;
                }

            }

        }

        private void PrintValue()
        {
            Debug.WriteLine(array[xIndex, yIndex]);
            traversed[xIndex, yIndex] = 1;
        }

        private bool CanTraverse()
        {
            if (CanMoveEast() || CanMoveNorth() || CanMoveSouth() || CanMoveWest())
            {
                return true;
            }
            return false;
        }

        private bool CanMoveSouth()
        {
            if ((xIndex == ArrayBound - 1) || (traversed[xIndex + 1, yIndex] == 1))
                return false;

            return true;
        }

        private bool CanMoveEast()
        {
            if ((yIndex == ArrayBound - 1) || (traversed[xIndex, yIndex + 1] == 1))
                return false;

            return true;
        }

        private bool CanMoveNorth()
        {
            if ((xIndex == 0) || (traversed[xIndex - 1, yIndex] == 1))
                return false;

            return true;
        }


        private bool CanMoveWest()
        {
            if ((yIndex == 0) || (traversed[xIndex, yIndex - 1] == 1))
                return false;

            return true;
        }

    }

- Mohit Goel March 05, 2014 | 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