Intelligrape Interview Question for Java Developers


Country: United States
Interview Type: In-Person




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

I'm guessing they want this to be flexible to accept larger negative values (e.g. what happens when the input array contains -4?). So,
1) find the minimum of the input array, if not negative, use 0.
2) print line by line

public void main(int[] inputArray) {
	int min = findMin(inputArray);

	for(int i=0;i<inputArray.length;i++){
		if(inputArray[i] > 0) {
			for(int j=min;j<0;j++){
				Console.Write(" ");
			}
		}
		else{
			for(int j=min;j<(min-inputArray[i]);j++){
				Console.Write(" ");
			}
		}
		for(int j=0;j<inputArray[i];j++){
			Console.Write("*");
		}
		Console.WriteLine();
	}
}

public int findMin(int[] array) {
	int minInt = array[0];
	for(int i=1;i<array.length;i++) {
		if(array[i] < minInt)
			{minInt=array[i];}
	}
	return minInt;
}

- john madden john madden john madden October 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

They ask not to find min or max.

- Anonymous October 11, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

wrong code

public class Print {
public static void main(String[] args) {
	int[] inputArray = {-5,2,-3,4,1};
	int min = findMin(inputArray);	
	
	for(int i=0;i<inputArray.length;i++){
		if(inputArray[i] > 0) {
			
			for(int j=min;j<inputArray[i];j++){
				if(j<0)
				System.out.print(" ");
				else 
				System.out.print("*");
			}
		}
		else{
			for(int j=min;j<0;j++){			
				if(j<inputArray[i])
				System.out.print(" ");
				else 
				System.out.print("*");
			}
			
		}
		
		System.out.println();
	}

}

public static int findMin(int[] array) {
	int minInt = array[0];
	for(int i=1;i<array.length;i++) {
		if(array[i] < minInt)
			{minInt=array[i];}
	}
	return minInt;
}
}

- Anonymous October 11, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void printStars(int[] inputArray) {            
            for (int value : inputArray) {            
              System.out.println((value<0?"":"  ")+(new char[value<0?-value:value]).toString().replace("\0", "*"));            
            }
        }
        
        public static final void main(String[] args) {
            
            int[] arr = {5,2,-3,4,1};
            printStars(arr);
        }

- eek October 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

wrong one:

System.out.println((value<0?"":" ")+new String (new char[value<0?-value:value]).replace("\0", "*"));

- eek October 11, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

oops wrong one:

System.out.println((value<0?"":" ")+new String (new char[value<0?-value:value]).replace("\0", "*"));

- eek October 11, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static void printArray(int[] inputArray) {
		String [] tempArrayA = new String[inputArray.length];
		String max = "";
		for (int i = 0; i < inputArray.length; i++) {
			if(inputArray[i]<0){
				tempArrayA[i] ="";
				for (int j = inputArray[i]; j < 0; j++) {
					tempArrayA[i] = tempArrayA[i] +" ";	
				}
				if(max.length()<tempArrayA[i].length()){
					max = tempArrayA[i];
				}
			}else{
				tempArrayA[i] ="";
				for (int j = 0; j < inputArray[i]; j++) {
					tempArrayA[i] = tempArrayA[i] +"*";
				}
			}
		}
		
		for (int i = 0; i < tempArrayA.length; i++) {
			if(tempArrayA[i].contains("*"))
				System.out.println(max + tempArrayA[i]);
			else
				System.out.println(tempArrayA[i]);
		}
	}

- AK October 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class New
{
public static void main(String args[])
{
int a[]={5,2,-3,4,1};
int min=a[0];
int spc=0;
for(int i=0;i<a.length;i++)
{
if(a[i]<min)
{
min=a[i];
}
}
System.out.println(min+"");
spc=min*-1;
System.out.println(spc+"");
for(int i=0;i<a.length;i++)
{ if(a[i]!=min)
{
for(int k=0;k<spc;k++)
{
System.out.print("_");
}
//System.out.println();
for(int j=0;j<a[i];j++)
{
System.out.print("*");
}
System.out.println();
}
else
{
for(int k=0;k<spc;k++)
{
System.out.print("*");
}
System.out.println();
}
}
}
}

- naveen kothiyal October 27, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package practice;

public class Test {


public static void main(String a[]){

int arr[] = {5,2,-3,4,1};
int m = arr[0];
int val;
for(int i = 1;i < arr.length;i++){
if(arr[i]<m){
m=arr[i];
}
}

m = m*(-1);

for(int i=0;i<arr.length;i++){
if(arr[i]>0){
val=arr[i];
for(int j=1;j<=m;j++){
System.out.print("_");
}
}else{
val=arr[i]*-1;
for(int j=1;j<=m+arr[i];j++){
System.out.print("_");
}
}

for(int j=0;j<val;j++){
System.out.print("*");
}
System.out.println();
}

}
}

- Nagendra Pratap Singh February 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Not very clean, but does the work..

Integer[] a = {5,2,-3,4,1};
		Integer smallest = a[0];
		for(int x = 0 ;x<a.length;x++)
		{
			if(smallest>a[x])
			{
				smallest = a[x];
			}
		}
		if(smallest<0)
		{
			smallest = smallest*(-1);
		}
		for(int x=0;x<a.length;x++)
		{
			System.out.println("\n");
			if(a[x]>0)
			{
				for(int y = 0;y<smallest;y++)
				{
					System.out.print(" ");
				}
				for(int y = 0 ; y< a[x];y++)
				{
					System.out.print("*");
				}
			}else if(a[x]<0)
			{
				Integer value = a[x]*(-1);
				Integer blanks = smallest - value;
				for(int y = 0;y<blanks;y++)
				{
					System.out.print(" ");
				}
				for(int y = 0 ; y< value;y++)
				{
					System.out.print("*");
				}
			}
		}

- Sohaib April 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Not very clean, but does the work..

Integer[] a = {5,2,-3,4,1};
		Integer smallest = a[0];
		for(int x = 0 ;x<a.length;x++)
		{
			if(smallest>a[x])
			{
				smallest = a[x];
			}
		}
		if(smallest<0)
		{
			smallest = smallest*(-1);
		}
		for(int x=0;x<a.length;x++)
		{
			System.out.println("\n");
			if(a[x]>0)
			{
				for(int y = 0;y<smallest;y++)
				{
					System.out.print(" ");
				}
				for(int y = 0 ; y< a[x];y++)
				{
					System.out.print("*");
				}
			}else if(a[x]<0)
			{
				Integer value = a[x]*(-1);
				Integer blanks = smallest - value;
				for(int y = 0;y<blanks;y++)
				{
					System.out.print(" ");
				}
				for(int y = 0 ; y< value;y++)
				{
					System.out.print("*");
				}
			}
		}

- Sohaib April 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Integer[] a = {5,2,-3,4,1};
		Integer smallest = a[0];
		for(int x = 0 ;x<a.length;x++)
		{
			if(smallest>a[x])
			{
				smallest = a[x];
			}
		}
		if(smallest<0)
		{
			smallest = smallest*(-1);
		}
		for(int x=0;x<a.length;x++)
		{
			System.out.println("\n");
			if(a[x]>0)
			{
				for(int y = 0;y<smallest;y++)
				{
					System.out.print(" ");
				}
				for(int y = 0 ; y< a[x];y++)
				{
					System.out.print("*");
				}
			}else if(a[x]<0)
			{
				Integer value = a[x]*(-1);
				Integer blanks = smallest - value;
				for(int y = 0;y<blanks;y++)
				{
					System.out.print(" ");
				}
				for(int y = 0 ; y< value;y++)
				{
					System.out.print("*");
				}
			}
		}

- Sohaib April 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.*;

public class pattern
{
    

public static void main(String[] args) {
	//code
	int []arr={5,2,-3,4,1};
	int i,j;
	for(i=0;i<5;i++)
	{
	    j=0;
	    if(arr[i]>0)
	    {
	      System.out.print("::");
	      while(j<arr[i])
	      {
	         System.out.print("*");
	         j++;
	      }
	    }
	    else
	    {
	    
	      while(j<(-1*arr[i]))
	      {
	           System.out.print("*");
	           j++;
	       }
	    }
	    System.out.println();
    }
 }
}

- XprSSd August 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Given an array arr=[5,2,-3,4,1]
print
_ _ * * * * *
_ _ * *
* * *
_ _ * * * *
_ _ *

Note [In real question, replace _ by blank(or space)]. So, while solving this question need to replace _ by blank ].

- vivek pratap singh September 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Pattern12
{
public static void main(String args[])
{
int i,j,k;
int a[]={5,2,-3,4,1};
int l=a.length;
for(i=0;i<l;i++)
{
if(a[i]>=0)
{
for(j=0;j<2;j++)
{
System.out.print("_");
}
for(k=0;k<a[i];k++)
{
System.out.print("*");
}
System.out.println("");
}
else
{
for(j=a[i];j<0;j++)
{
System.out.print("*");
}
System.out.println("");
}
}
}
}

- vviek pratap singh September 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Pattern12
{
public static void main(String args[])
{
int i,j,k;
int a[]={5,2,-3,4,1};
int l=a.length;
for(i=0;i<l;i++)
{if(a[i]>=0)
{for(j=0;j<2;j++)
{System.out.print("_");}
for(k=0;k<a[i];k++)
{System.out.print("*");}
System.out.println("");}
else
{
for(j=a[i];j<0;j++)
{System.out.print("*");}
System.out.println("");
}}}}

- vviek pratap singh September 08, 2015 | 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