Interview Question


Country: India




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

private static void printsq(int n) {
         if (n <= 0) {
             return;
         }
         if (n == 1) {
             System.out.println("1");
             return;
         }
         int[][] arr = new int[n][n];
         int ctr = 1;
         for (int i = 1; i <= n/2; i++) {
             for (int j = 1; j<=n; j++) {
                arr[i-1][j-1] = ctr;
                ctr++;
             }
             for(int j = 1; j <= n; j++) {
                 arr[n-i][j-1] = ctr;
                 ctr++;
             }
         }
        
        if (n%2 == 1) {
            for (int j =0; j < n; j++) {
                arr[n/2][j] = ctr;
                ctr++;
            }
        }
         
        for(int i=0; i<n; i++) {
            for(int j=0;j<n;j++) {
                if (j==0)
                    System.out.print(arr[i][j]);
                else
                    System.out.print(" * " + arr[i][j]);
            }
            System.out.println();
        }
     }

- Swap October 10, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

// ZoomBA
def print_pattern( n ){
  if ( n < 2 ) { println ( n ) ; return }
  // create n * n matrix
  m = list( [0:n] ) -> { list() }
  // 0 , n-1, 1, n - 2 ...
  row_indices = lfold ( [ 1:n-1],  list(0,n-1) ) ->{
      $.partial += ( $.partial[-2] + ( (-1) ** $.index) )
  }
  count = 1
  for ( row : row_indices ){
    for ( [0:n ] ){
      m[row] += count
      count +=1
    }
  }
  lfold(m) -> { println ( str( $.item, ' * ' ) ) }
}
// use it
print_pattern ( 3 )

- NoOne October 10, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void printsq(int n) {
         if (n <= 0) {
             return;
         }
         if (n == 1) {
             System.out.println("1");
             return;
         }
         int[][] arr = new int[n][n];
         int ctr = 1;
         for (int i = 1; i <= n/2; i++) {
             for (int j = 1; j<=n; j++) {
                arr[i-1][j-1] = ctr;
                ctr++;
             }
             for(int j = 1; j <= n; j++) {
                 arr[n-i][j-1] = ctr;
                 ctr++;
             }
         }
        
        if (n%2 == 1) {
            for (int j =0; j < n; j++) {
                arr[n/2][j] = ctr;
                ctr++;
            }
        }
         
        for(int i=0; i<n; i++) {
            for(int j=0;j<n;j++) {
                if (j==0)
                    System.out.print(arr[i][j]);
                else
                    System.out.print(" * " + arr[i][j]);
            }
            System.out.println();
        }
     }

- Anonymous October 10, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

write 1 to n in first row.
add n*2 in each column in next rows till mid row.
after that substract n in each column.
and further row substract n*2 from each column. likewise.

- vivek agrawal October 10, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>

void printMatrix(int matrice1[100][100], int n, int rem1, int rem2)
{
int i=0;
int j=0;

for( i=0; i<n; i++)
{
for (j=0; j<n; j++)
{
if (i == 0) /* complete the first line o the matrix */
{
matrice1[0][j] = j + 1;
}
else if ((i > 0) && (i <= n/2)) /* complete the lines from 1 to n/2 */
{
matrice1[i][j] = rem1 + n + j + 1;
if (j==n-1)
{
rem1 = matrice1[i][j]; /* rem1 value set to the last element of the line */
}
}
else if (i == n-1) /* complete the last line of the matrix */
{
matrice1[i][j] = n+j+1;
}
else if ((i > n/2) && (i < n)) /* complete the lines between n/2 + 1 and n-1 */
{
matrice1[i][j] = rem2 + n + j + 1;
if (j==n-1)
{
rem2 = matrice1[i][j];
}
}

}
}

/* print the final matrix */
for (i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
printf("%d " , matrice1[i][j]);
}
printf("\n");
}
}

int main()
{
int nbofElements;
int matrice[100][100]={0};
int reminder1;
int reminder2;

printf("Introduceti un numar impar!");
scanf("%d", &nbofElements);

if (nbofElements % 2 == 0)
{
printf("Introduceti un numar impar!");
scanf("%d", &nbofElements);
if (nbofElements % 2 == 0)
{
exit(0); /* if the user enter a second wrong number -> exit the program */
}
}
else
{

}

reminder1 = nbofElements;
reminder2 = nbofElements*2; /*it has the value of the n*2 */

printMatrix(matrice, nbofElements, reminder1, reminder2);
getchar();

return 0;
}

- Precup October 14, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>

void printMatrix(int matrice1[100][100], int n, int rem1, int rem2)
{
	int i=0;
	int j=0;

	for( i=0; i<n; i++)
	{
		for (j=0; j<n; j++)
		{
			if (i == 0) /* complete the first line o the matrix */
			{
				matrice1[0][j] = j + 1;
			}
			else if ((i > 0) && (i <= n/2)) /* complete the lines from 1 to n/2 */
			{
				matrice1[i][j] = rem1 + n + j + 1;
				if (j==n-1)
				{
					rem1 = matrice1[i][j]; /* rem1 value set to the last element of the line */
				}
			}
			else if (i == n-1) /* complete the last line of the matrix */
			{
				matrice1[i][j] = n+j+1;  
			}
			else if ((i > n/2) && (i < n)) /* complete the lines between n/2 + 1 and n-1 */
			{
				matrice1[i][j] = rem2 + n + j + 1;
				if (j==n-1)
				{
					rem2 = matrice1[i][j]; 
				}
			}

		}
	}

	/* print the final matrix */
	for (i=0; i<n; i++)
	{
		for(j=0; j<n; j++)
		{
			printf("%d " , matrice1[i][j]);
		}
		printf("\n");
	}
}

int main()
{
      int nbofElements;
	  int matrice[100][100]={0}; 
	  int reminder1;
	  int reminder2;
	  
	  printf("Introduceti un numar impar!");
	  scanf("%d", &nbofElements);

	  if (nbofElements % 2 == 0)
	  {		   
		  printf("Introduceti un numar impar!");
		  scanf("%d", &nbofElements);
		  if (nbofElements % 2 == 0)
		  {
			  exit(0); /* if the user enter a second wrong number -> exit the program */
		  }
	  }
	  else 
	  {

	  }
	  
	  reminder1 = nbofElements; 
	  reminder2 = nbofElements*2; /*it has the value of the n*2  */

	  printMatrix(matrice, nbofElements, reminder1, reminder2);  
	  getchar();

      return 0;

- Precup October 14, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>

void printMatrix(int matrice1[100][100], int n, int rem1, int rem2)
{
	int i=0;
	int j=0;

	for( i=0; i<n; i++)
	{
		for (j=0; j<n; j++)
		{
			if (i == 0) /* complete the first line o the matrix */
			{
				matrice1[0][j] = j + 1;
			}
			else if ((i > 0) && (i <= n/2)) /* complete the lines from 1 to n/2 */
			{
				matrice1[i][j] = rem1 + n + j + 1;
				if (j==n-1)
				{
					rem1 = matrice1[i][j]; /* rem1 value set to the last element of the line */
				}
			}
			else if (i == n-1) /* complete the last line of the matrix */
			{
				matrice1[i][j] = n+j+1;  
			}
			else if ((i > n/2) && (i < n)) /* complete the lines between n/2 + 1 and n-1 */
			{
				matrice1[i][j] = rem2 + n + j + 1;
				if (j==n-1)
				{
					rem2 = matrice1[i][j]; 
				}
			}

		}
	}

	/* print the final matrix */
	for (i=0; i<n; i++)
	{
		for(j=0; j<n; j++)
		{
			printf("%d " , matrice1[i][j]);
		}
		printf("\n");
	}
}

int main()
{
      int nbofElements;
	  int matrice[100][100]={0}; 
	  int reminder1;
	  int reminder2;
	  
	  printf("Introduceti un numar impar!");
	  scanf("%d", &nbofElements);

	  if (nbofElements % 2 == 0)
	  {		   
		  printf("Introduceti un numar impar!");
		  scanf("%d", &nbofElements);
		  if (nbofElements % 2 == 0)
		  {
			  exit(0); /* if the user enter a second wrong number -> exit the program */
		  }
	  }
	  else 
	  {

	  }
	  
	  reminder1 = nbofElements; 
	  reminder2 = nbofElements*2; /*it has the value of the n*2  */

	  printMatrix(matrice, nbofElements, reminder1, reminder2);  
	  getchar();

      return 0;
}

- Precup October 14, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
void printMatrix(int matrice1[100][100], int n, int rem1, int rem2)
{
int i=0;
int j=0;
for( i=0; i<n; i++)
{
for (j=0; j<n; j++)
{
if (i == 0) /* complete the first line o the matrix */
{
matrice1[0][j] = j + 1;
}
else if ((i > 0) && (i <= n/2)) /* complete the lines from 1 to n/2 */
{
matrice1[i][j] = rem1 + n + j + 1;
if (j==n-1)
{
rem1 = matrice1[i][j]; /* rem1 value set to the last element of the line */
}
}
else if (i == n-1) /* complete the last line of the matrix */
{
matrice1[i][j] = n+j+1;
}
else if ((i > n/2) && (i < n)) /* complete the lines between n/2 + 1 and n-1 */
{
matrice1[i][j] = rem2 + n + j + 1;
if (j==n-1)
{
rem2 = matrice1[i][j];
}
}
}
}
/* print the final matrix */
for (i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
printf("%d " , matrice1[i][j]);
}
printf("\n");
}
}
int main()
{
int nbofElements;
int matrice[100][100]={0};
int reminder1;
int reminder2;
printf("Introduceti un numar impar!");
scanf("%d", &nbofElements);
if (nbofElements % 2 == 0)
{
printf("Introduceti un numar impar!");
scanf("%d", &nbofElements);
if (nbofElements % 2 == 0)
{
exit(0); /* if the user enter a second wrong number -> exit the program */
}
}
else{}
reminder1 = nbofElements;
reminder2 = nbofElements*2; /*it has the value of the n*2 */
printMatrix(matrice, nbofElements, reminder1, reminder2);
getchar();
return 0;
}

- Precup October 14, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int N = in.nextInt();
		in.close();
		for (int j = 0, c = 2; j != -1;) {
			if (j != N) {
				for (int i = 1; i <= N; i++)
					System.out.printf("%d*", j*N+i);
				System.out.printf("\b\n");

			}
			j += c;
			if (j == N || j == N + 1) {
				j--;
				c = -2;
			}
		}
	}
}

- Ch.v.sai kiran November 04, 2016 | 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