Sunday, 13 October 2013

Maventic Technical Question

Technical Round Question (Pattern in C/C++)

"Hey friends, in one of the recent interviews (Technical Round 2) of Maventic, the following design pattern was asked:



In this article, I'm going to explain on how you can easily produce this design pattern in C programming language

If you are familiar with the programming languages. Then this is very easy to understand this concept.

now a days many companies asking this concept in Technical round,
so we need to clear this concept to get the place.


Question : Write a program to design the pattern based on
           value(n should be odd number)
      ex :
           n=9
           output:


Sol:
 See it is very easy to design you have to analyse the pattern.
 This is the combination of * and blank space.
 we have to  identify that in which position we need to print *
 and don't worry about the blank spaces.

 we can analyse it like:
[ "-" represent a blank space][ and n=9]

here we can solve this in some steps:--

 (i) this program is a shape of matrix.
     so we need to use two for loop to solve this problem.
   
     ex1: for(i=1;i<=n;i++)
           {
             for(j=1;j<=n;j++)
              {
                  if(condition)
                         printf(" *");
                        else
                         printf("  ");//blank space
                   
              }
             printf("\n");
           }

(ii) ith 1st and n(9)th index jth 1st and n(9)th index you see.
    each position we need to print '*' .
    in side loop condition  we can write it as..
 
    ex2:
      if(i==1 || i==n || j==1 || j==n)
         printf(" *");
       else
        printf("  ");

    then output come like box.
   


 (iii) now we need to draw like:
   
      here we can see it's printing * where-where ith index and
      jth index are equals except middle mean [(n/2)+1=5]
      so we can write code for this:
 
     eg3:
        if(i==j && j!=(n/2)+1)
         printf(" *");
       else
         printf("  ");

 (iv)Now last pattern, reverse of step(iii) pattern..
     ex:
   
     so here we can analyse it's decreasing order of j index
     9,8,7,6,4,3,2,1 except middle means [n/2+1=5]
   
     For this we can write the condition:-

     eg4:
       if(j==(n+1)-i && j!=(n/2)+1)
          printf(" *");
        else
          printf("  ");

In above example we declared the some variation of pattern based on condition...
Even we combined ex:3 and ex:4 condition then we will get output like:
   
condition:
    ex5:
       if(i==j && j!=(n/2)+1 || j==(n+1)-i && j!=(n/2)+1)
          printf(" *");
        else
          printf("  ");

And finally Even we combined this ex5 with ex2 then we will get output like:

    Final Example:
             

so finally code I am writing below:

#include<stdio.h>
#include<conio.h>

void main()
{
 int n;
 clrscr();
 printf("Enter num: ");
  scanf("%d",&n);

 for( i=1;i<=n;i++)
 {
for(int j=1;j<=n;j++)
 {

if(i==1 || j==1 || i==n|| j==n || i==j && j!=(n/2)+1 || j==((n+1)-i) && j!=n/2+1)

{
     printf(" *");
  }
 else
      printf("  ");
}
 printf("\n");
 }

 getch();
}

output::based on this code..


so friends I hope you understood these concept easily.
if you have any query then please ask to me and please give your valuable comments.
thank you.....



21 comments:

  1. Replies
    1. Your most welcome bro...Thanks....for your Comments....

      Delete
  2. nyc work.. Can you giv me some more pattern question asked in maventic.. thnks in advn

    ReplyDelete
  3. can u suggest us which type of questions we expect from maventic

    ReplyDelete
    Replies
    1. Most probably in Maventic, they will ask pattern based question... and I am telling to one true thing of companies... mostly companies they ask logical based question... those question you can able to make using any programming language... so it's mean that ...only logical or pattern based question... becs they want to know who's logic is good... It's does not matter which language you are using..to do it... I hope it will help you...all the best...

      Delete
  4. how to write same code in java

    ReplyDelete
  5. Thank you so much for giving me a such nice hint to make that type of pataran

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. really so nice explanation

    ReplyDelete

  9. public class Pattern {

    public static void main(String[] args)
    {
    int no=15;
    int n=(no+1)/2;
    int k=no;
    for(int i=1;i<=no;i++)
    {
    k--;
    for(int j=1;j<=no;j++)
    {


    if(i==1 || i==no || j==1 || j==no || i==j || j==k+1)
    {
    if(i==n && (j!=1 && j!=no) )
    System.out.print(" ");
    else
    System.out.print("*");
    }
    else
    System.out.print(" ");
    }
    System.out.println();
    }
    }

    }

    ReplyDelete