AceTheInterview
Jobs in Pune | Work better in teams | Socialize with friends | Submit Q&A | Tell a friend
Search site for 

Top 100 Interview Questions & Answers in a convenient and easy to read book!

“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”

– Ravi, California

Read more comments...

Interview Questions And Answers RSS Feed

Answers »

  1. Submitted By: zanic — October 6, 2006
    +0 votes
      + -

    I don’t think I could write the correct code for this question in 15 minutes. The code below only show clockwise printing result. You are welcomed to optimize or generalize it.

    #include <stdio.h>

    char test[5][5] = { “ABCD”, “ABCD”, “ABCD”, “ABCD” };

    void main() {
    int x_count = 4, x_limit = 4, y_count = 4, y_limit = 4;
    int pivol = 0, // x changed = 0, y changed = 1
    i = 0,
    j = 0,
    prev_i = 1,
    prev_j = 1,
    temp;

    while (x_limit != 0 && y_limit != 0) {
    if (pivol == 0) {
    if (x_count == x_limit) {
    putchar(test[i][j]);
    pivol = 1;
    y_count = 1;
    y_limit–;
    temp = j;
    j = prev_j;
    prev_j = temp;
    continue;
    }
    if (prev_i <= i) {
    putchar(test[i][j]);
    prev_i = i; i++; x_count++;
    continue;
    }
    if (prev_i > i) {
    putchar(test[i][j]);
    prev_i = i; i–; x_count++;
    }
    }
    if (pivol == 1) {
    if (y_count == y_limit) {
    putchar(test[i][j]);
    pivol = 0;
    x_count = 1;
    x_limit–;
    temp = i;
    i = prev_i;
    prev_i = temp;
    continue;
    }
    if (prev_j <= j) {
    putchar(test[i][j]);
    prev_j = j; j++; y_count++;
    continue;
    }
    if (prev_j > j) {
    putchar(test[i][j]);
    prev_j = j; j–; y_count++;
    }
    }
    }
    }

  2. Submitted By: ssanand — October 6, 2006
    -4 votes
      + -

    What do you mean by printing array in spiral fashion??

    If you have 2D array as

    1 2 3 4 5
    2 3 4 5 6
    3 4 5 6 7
    4 5 6 7 8
    5 6 7 8 9

    Does spiral printing mean ” 1 2 2 3 3 3 4 4 4… and so on? or printing 12345 23456 3456….

  3. Submitted By: zanic — October 6, 2006
    -1 votes
      + -

    To ssanand:

    You may probe what the interview intends to ask you.

  4. Submitted By: Seven — October 6, 2006
    +1 votes
      + -

    Hi,
    This is what I think is Spiral Traversal -
    If you were given an array like:
    A B C D E
    1 2 3 4 5
    A B C D E
    1 2 3 4 5
    The output should be: ABCDE5E54321A1234DCB
    Key: All the elements of the array should be covered.
    Can you modify the code accordingly zanic?
    Thx!

  5. Submitted By: mkhambatti — October 6, 2006
    +2 votes
      + -

    This program works !!!
    I have hard-coded a 4×4 matrix called arr
    Run the program and see the results..
    Anytime you want to use a different size matrix.. just change the row and col values that I initialized below..works for all cases that I tested..

    int main ()
    {
    int arr[][4] = { {1,2,3,4}, {5,6,7,8}, {9,10,11,12}, {13, 14, 15, 16}};

    int i = 0, j = 0, row = 4, col = 3, done_i = 0, done_j = 0;
    int count = row*col;

    for (; i < row-done_i; i++)
    {
    for (; j < col-done_j; j++)
    {
    cout << arr[i][j] << ” “;
    cout.flush(); count–;
    }

    for (j –, i++; i < row-done_i; i++)
    {
    cout << arr[i][j] << ” “;
    cout.flush(); count–;
    }

    done_i++;

    if (count <= 0)
    break;

    for (i–, j–; j >= done_j; j–)
    {
    cout << arr[i][j] << ” “;
    cout.flush(); count–;
    }

    for (j++, i–; i >= done_i; i–)
    {
    cout << arr[i][j] << ” “;
    cout.flush(); count–;
    }

    done_j++;
    j++;
    }

    return 1;
    }

  6. Submitted By: goodguy — October 6, 2006
    +3 votes
      + -

    Here is my universal solution. You can just copy and paste to run:

    static void spiralRoute()
    {
    char a[7][7] = {
    {’a', ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’},
    {’h', ‘i’, ‘j’, ‘k’, ‘l’, ‘m’, ‘n’},
    {’o', ‘p’, ‘q’, ‘r’, ’s’, ‘t’, ‘u’},
    {’1′, ‘2′, ‘3′, ‘4′, ‘5′, ‘6′, ‘7′},
    {’O', ‘P’, ‘Q’, ‘R’, ‘S’, ‘T’, ‘U’},
    {’H', ‘I’, ‘J’, ‘K’, ‘L’, ‘M’, ‘N’},
    {’A', ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’},
    };
    char rightoutput[50] = “abcdefgnu7UNGFEDCBAHO1ohijklmt6TMLKJIP2pqrs5SRQ34″;
    char myoutput[50];

    /* DIRECTIONS:
    0: towarding right;
    1: towarding down;
    2: towarding left;
    3: towarding up;
    */
    int direction = 0;
    int up_ = 0, down_ = 6, right_ = 6, left_ = 0;
    int i, counter = 0, bOver = 0;

    while (!bOver) {
    switch (direction)
    {
    case 0:
    if (right_ < left_)
    {
    bOver = 1;
    } else {
    for (i = left_; i <= right_; i++)
    {
    myoutput[counter++] = a[up_][i];
    }
    up_++;
    direction++;
    }
    break;
    case 1:
    if (down_ < up_)
    {
    bOver = 1;
    } else {
    for (i = up_; i <= down_; i++)
    {
    myoutput[counter++] = a[i][right_];
    }
    right_–;
    direction++;
    }
    break;
    case 2:
    if (right_ < left_)
    {
    bOver = 1;
    } else {
    for (i = right_; i >= left_; i–)
    {
    myoutput[counter++] = a[down_][i];
    }
    down_–;
    direction++;
    }
    break;

    case 3:
    if (down_ < up_)
    {
    bOver = 1;
    } else {
    for (i = down_; i >= up_; i–)
    {
    myoutput[counter++] = a[i][left_];
    }
    left_++;
    direction = 0;
    }
    break;
    }
    }
    myoutput[counter] = ‘’;

    printf(”rightoutput = %sn”, rightoutput);
    printf(”myoutput %sn”, myoutput);
    if (strcmp(rightoutput, myoutput) == 0) {
    printf(”passedn”);
    } else {
    printf(”not passedn”);
    }
    }

  7. Submitted By: LiEf — October 6, 2006
    +12 votes
      + -

    void print_spiral(int** arr, int size)
    {
    int i,j,k,middle;

    for(i=size-1, j=0; i>0; i–, j++) {

    for(k=j; k<i; k++)
    printf(”%d”, arr[j][k]);

    for(k=j; k<i; k++)
    printf(”%d”, arr[k][i]);

    for(k=i; k>j; k–)
    printf(”%d”, arr[i][k]);

    for(k=i; k>j; k–)
    printf(”%d”, arr[k][j]);
    }

    middle = (n-1)/2;

    if (n % 2 == 1)
    printf(”%d”, arr[middle][middle]);

    printf(”n”);
    }

  8. Submitted By: Alexander_V — October 16, 2006
    -2 votes
      + -

    # include

    int arr[4][4] = {
    {1,2,3,4},
    {5,6,7,8},
    {9,10,11,12},
    {13, 14, 15, 16}
    };

    typedef unsigned int uint;

    void main()
    {
    for( uint size=4, idx=0; size>0; size-=2, ++idx )
    {
    for( uint i=0; i0; –ii ) cout 1; –jj ) cout

  9. Submitted By: Gaurav Ganeriwal — November 18, 2006
    +0 votes
      + -

    Algo for spiral traversal can be as follows

    int Switch = 1;
    int max_row = MAXROW;
    int max_col = MAXCOL;
    int i = j = 0;
    while ( max_col > 0 or max_row > 0 )
    {
    for( k = 0 ; k

  10. Submitted By: fatih — March 14, 2007
    +0 votes
      + -

    void printShape(char **bas,int x, int y)
    {
    int k =0;

    while(x>1 && y>1)
    {
    //Sag
    for(int i=k; ik; i–)
    printf(”%c “,bas[y-1+k][i]);
    //Yukari
    for(int i=y-1+k; i>k; i–)
    printf(”%c “,bas[i][k]);

    x= x - 2;
    y= y - 2;
    k++;
    }

    if(x==1 && y==1){
    printf(”%c “,bas[0][0]);
    }

    return;
    }

  11. Submitted By: f — March 14, 2007
    +1 votes
      + -

    void printShape(char **bas,int x, int y)
    {
    int k =0;

    while(x>1 && y>1)
    {
    //Sag
    for(int i=k; ik; i–)
    printf(”%c “,bas[y-1+k][i]);
    //Yukari
    for(int i=y-1+k; i>k; i–)
    printf(”%c “,bas[i][k]);

    x= x - 2;
    y= y - 2;
    k++;
    }

    if(x==1 && y==1){
    printf(”%c “,bas[0][0]);
    }

    return;
    }

  12. Submitted By: FanZai — July 3, 2007
    -1 votes
      + -

    in VB 6.0:

    Private Function Min(ByVal a As Integer, ByVal b As Integer)
    If a

  13. Submitted By: Ajay Mathur — July 8, 2007
    +0 votes
      + -

    In Java, the correct solution for above is like this it can work for n by n matix or 2d array. Just replace with correct values:

    public class PrintCircular
    {
    public static void main(String args[])
    {
    final int right = 1;
    final int down = -1;
    final int left = 2;
    final int up = -2;
    int arr[][] = { {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,26,27,28,29,30},

    {31,32,33,34,35,36}};
    int count = 36;
    int i = 0, j=0, minx = 0, maxx = 5, miny = 0, maxy = 5;
    int direction = right;
    while(count>0)
    {
    System.out.print(arr[i][j] + ” “);
    if(direction == right)
    j++;
    else if(direction == down)
    i++;
    else if(direction == left)
    j–;
    else
    i–;

    if(j==miny && i==minx)
    direction = right;
    else if(j==maxy && i==minx)
    direction = down;
    else if(i==maxx && j==maxy)
    direction = left;
    else if(i==maxx && j==miny)
    direction = up;

    if(i==minx && j==miny)
    {
    minx++;
    maxy–;
    miny++;
    maxx–;
    i=minx;
    j=miny;
    }
    System.out.print(”minx =” + minx + ” maxx = ” + maxx + ” miny = ” + miny + ” maxy = ” + maxy + ” i = ” + i

    + ” j =” +j + “\n”);
    count –;
    }
    }
    }

  14. Submitted By: JosephLee — August 20, 2007
    +1 votes
      + -

    (C++and recursive, based on #8)
    template
    void print_spiral(T** arr,int start,int row,int col)
    {
    if((start right
    for(int j = start ; j bottom
    for(int i = start+1; i left
    for(int j = col -1 ; j > start ; j–)
    cout top
    for(int i = row -1 ;i > start; i–)
    cout bottom
    for(int i = start; i

  15. Submitted By: Deepak Garg — September 3, 2007
    -1 votes
      + -

    function TravMatrix( int matrix[][], int row , int col )
    {
    variable int rowU = 0, rowD = 0, colL = 0,colR = 0,total = 0,r=0,c=0;

    1. total = rowU + rowD + colL + colR ;
    2. while total != ( row + col )
    do
    1. if rowTraverseRight = true then
    1. for ( ; c colR ; c– )
    do
    2. print matrix[r][c];
    done
    3. rowD++;
    4. colTraverseUp = true;
    5. rowTraverseRight = colTraverseDown = rowTraverseLeft = false;
    4. else if colTraverseUp = true then
    1. for ( ; r > rowD ; r– )
    do
    2. print matrix[r][c];
    done
    3. colL++;
    4. rowTraverseRight = true;
    5. colTraverseDown = rowTraverseLeft = colTraverseUp = false;
    5. total = rowU + rowD + colL + colR ;
    done
    3. Exit From Function.
    }

  16. Submitted By: Michal — February 21, 2008
    not yet rated
      + -

    enum DIRECTION {
    DOWN, RIGHT, UP, LEFT
    }

    public static void arrayAsSpiral(Object[][] P_array) {

    int x_size = P_array.length;
    int y_size = 0;

    for (Object[] obj : P_array) {
    if (obj.length > y_size) {
    y_size = obj.length;
    }
    }

    int x_min = 0;
    int x_max = x_size - 1;
    int y_min = 0;
    int y_max = y_size - 1;

    int p_x = 0;
    int p_y = 0;

    DIRECTION dir = DIRECTION.DOWN;
    System.out.print(”[” + P_array[p_x][p_y] + “]”);

    while ((y_max >= y_min) && (x_max >= x_min)) {

    switch (dir) {
    case DOWN:
    if (p_y y_min) {
    p_y–;
    } else {
    dir = DIRECTION.LEFT;
    x_max–;
    continue;
    }
    break;
    case LEFT:
    if (p_x > x_min) {
    p_x–;
    } else {
    dir = DIRECTION.DOWN;
    y_min++;
    continue;
    }
    break;
    }

    System.out.print(”[” + P_array[p_x][p_y] + “]”);

    }

    }

  17. Submitted By: omkar — March 18, 2008
    not yet rated
      + -

    omkar
    dot net code

    public static void zigzag(int[,] array,int maxRow,int maxCol)
    {
    int row = 0,col=0;
    bool flag = false;
    for (int i = 0; i = 0; j–)
    {
    row = i - j;
    col = j;
    if (row

  18. Leave an Answer/Comment

    To prove you're a person (not a spam script), type the security text shown in the picture. Click here to regenerate some new text.
    Click to hear an audio file of the anti-spam word

Our Sponsors
Our Sponsors
Contact Us | FAQ | Sitemap | Terms of Use | Privacy Policy | Tell a Friend

Copyright © 1999-2006 Jeeve Technologies LLC. All rights reserved.