Amazon Interview Question for Software Engineer Interns

• 0

Country: United States
Interview Type: Phone Interview

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

``````m = max of array a;
n = size of n;
for (i=m; i>0; i--){
for (j=0; i<n; j++){
printf("%c", a[j]>=i ? '#' : ' ');
}
printf("\n");
}``````

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

what if the number are random integers in array, so I would like to sort it in ascending order first and then print
1) sort array low to high
2) print them

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

Sorry, there are typos:
1) "i>;0" in the 3rd line should be "i>0".
2) "i<n" in the 4th line should be "j<n".

And.. you don't need to sort. The order of values in the array does not matter.

The algorithm is very simple.
1. Find the maximum value (m) in the array.
2. Let i=m;
3. For j=1,2,..,n, if a[j] is no less than i, then print "#". Else print " ".
4. i=i-1 and goto 2.

I think the output in the original post is misleading. I think it should be like

``````#
##
###
####``````

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

As other posters said:

``````//   #
//   ##
//   ###
// ####

#include <stdio.h>

int findMax(int *ints, int cInts) {
int max = 0;
for (int iInt = 0; iInt < cInts; iInt++) {
if (ints[iInt] > max) {
max = ints[iInt];
}
}
return max;
}

void printHashes(int *ints, int cInts) {
int max = findMax(ints, cInts);
for (int row = 0; row < max; row++) {
for (int iInt = 0; iInt < cInts; iInt++) {
if (ints[iInt] >= (max - row)) {
printf("#");
} else {
printf(" ");
}
}
printf("\r\n");
}
}

int main(int argc, char **argv) {
int ints[]  = { 1, 4, 3, 2 };
printHashes(ints, sizeof(ints)/sizeof(int));
return 0;
}``````

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

``````static void printHash (int [] A){
int len = A.length;
int [] countArray = new int[len];
for(int a : A){
countArray[len - a]++;
}
int previousCount = 0;
for(int a : countArray){
previousCount += a;
for(int i=0;i<previousCount;i++){
System.out.print("# ");
}
System.out.println();
}
}``````

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

you code fails bro...

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

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

hi Mudit,
Where does it fails.

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

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

``````static void print(int[] arr)
{ int max = 0;
for (int a:arr )
{
if (a > max)
{
max = a;
}
}

for (int i = 0; i < max ; i++)
{
for (int j=0; j<arr.length;j++)
{
if ((max-i)<=arr[j])
{
System.out.print("#");

}
else
{
System.out.print(" ");
}

}
System.out.println();
}

}``````

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

``````// 1. Array input and 2D array creation
int[] arr = { 7,2,1,5,3,2,1 };
int maxNumber = arr.Max();
char[][] displayPattern = new char[maxNumber][];
for (int i = 0; i < maxNumber; i++)
{
displayPattern[i] = new char[maxNumber];
}

// 2. Putting # wherever applicable
for (int i = 0; i < arr.Length; i++)
{
int j = arr[i];
for (; j > 0; j--)
{
displayPattern[maxNumber - j][i] = '#';
}
for (; j > 0; j--)
{
displayPattern[j][i] = ' ';
}
}

// 3. Print it
for (int i = 0; i < maxNumber; i++)
{
for (int j = 0; j < maxNumber; j++)
{
Console.Write(displayPattern[i][j]);
}
Console.WriteLine();
}``````

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

``````// 1. Array input and 2D array creation
int[] arr = { 7,2,1,5,3,2,1 };
int maxNumber = arr.Max();
char[][] displayPattern = new char[maxNumber][];
for (int i = 0; i < maxNumber; i++)
{
displayPattern[i] = new char[maxNumber];
}

// 2. Putting # wherever applicable
for (int i = 0; i < arr.Length; i++)
{
int j = arr[i];
for (; j > 0; j--)
{
displayPattern[maxNumber - j][i] = '#';
}
for (; j > 0; j--)
{
displayPattern[j][i] = ' ';
}
}

// 3. Print it
for (int i = 0; i < maxNumber; i++)
{
for (int j = 0; j < maxNumber; j++)
{
Console.Write(displayPattern[i][j]);
}
Console.WriteLine();
}``````

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

``````// 1. Array input and 2D array creation
int[] arr = { 7,2,1,5,3,2,1 };
int maxNumber = arr.Max();
char[][] displayPattern = new char[maxNumber][];
for (int i = 0; i < maxNumber; i++)
{
displayPattern[i] = new char[maxNumber];
}

// 2. Putting # wherever applicable
for (int i = 0; i < arr.Length; i++)
{
int j = arr[i];
for (; j > 0; j--)
{
displayPattern[maxNumber - j][i] = '#';
}
for (; j > 0; j--)
{
displayPattern[j][i] = ' ';
}
}

// 3. Print it
for (int i = 0; i < maxNumber; i++)
{
for (int j = 0; j < maxNumber; j++)
{
Console.Write(displayPattern[i][j]);
}
Console.WriteLine();
}``````

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

``````// 1. Array input and 2D array creation
int[] arr = { 7,2,1,5,3,2,1 };
int maxNumber = arr.Max();
char[][] displayPattern = new char[maxNumber][];
for (int i = 0; i < maxNumber; i++)
{
displayPattern[i] = new char[maxNumber];
}

// 2. Putting # wherever applicable
for (int i = 0; i < arr.Length; i++)
{
int j = arr[i];
for (; j > 0; j--)
{
displayPattern[maxNumber - j][i] = '#';
}
for (; j > 0; j--)
{
displayPattern[j][i] = ' ';
}
}

// 3. Print it
for (int i = 0; i < maxNumber; i++)
{
for (int j = 0; j < maxNumber; j++)
{
Console.Write(displayPattern[i][j]);
}
Console.WriteLine();
}``````

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

Your two for loops in a row

``for (; j > 0; j--)``

subtracts j all the way down to 0.
That means by the time you hit your second loop j is already 0 so the second for loop will not even run.

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

Yes, I put it in hurry :P. Following is the updated version.
----
// 1. Array input and 2D array creation
int[] arr = { 7,2,1,5,3,2,1 };
int maxNumber = arr.Max();
char[][] displayPattern = new char[maxNumber][];
for (int i = 0; i < maxNumber; i++)
{
displayPattern[i] = new char[maxNumber];
}

// 2. Putting # wherever applicable
for (int i = 0; i < arr.Length; i++)
{
int j = arr[i];
for (; j > 0; j--)
{
displayPattern[maxNumber - j][i] = '#';
}
}

// 3. Print it
for (int i = 0; i < maxNumber; i++)
{
for (int j = 0; j < maxNumber; j++)
{
Console.Write(displayPattern[i][j]);
}
Console.WriteLine();
}

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

this is a question asking do you know insertion sort.

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

Idea: Construct horizontal layers of the chart from the bottom up. Print the layers in the reverse order to print them top to bottom.
For better performance in larger charts, use a StringBuffer to build up layers.

``````void print(int [] a) {
// Build up layers of chart bottom to top
ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < a.length; i++) {
String s = ""; // This holds the current layer
if (a[i] > 0) {
s += "#"; // Bar still under construction; increase it by one "#"
a[i]--;
} else {
s += " "; // Once a vertical bar is fully constructed, just insert spaces
}
}

// Print layers in reverse order (top to bottom)
for (int i = list.length - 1; i >= 0; i--) {
System.out.println(list.get(i));
}
}``````

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

Can't you use treeset?

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

for(int i=0;i<a.length;i++)
{
for(int j=1;j<=a[i];j++)

System.out.print("*");

System.out.println();
}

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

{for(int i=0;i<a.length;i++)
{
for(int j=1;j<=a[i];j++)

System.out.print("*");

System.out.println();
}
}

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

for(int i=0;i<a.length;i++)
{
for(int j=1;j<=a[i];j++)

System.out.print("*");

System.out.println();
}

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

``````for(int i=0;i<a.length;i++)
{
for(int j=1;j<=a[i];j++)

System.out.print("*");

System.out.println();``````

}

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

``````for(int i=0;i<a.length;i++)
{ for(int j=1;j<=a[i];j++)
System.out.print("*"); System.out.println();``````

}

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

public void print(int[] A){
if(A.length < 1)
return;
Arrays.sort(A);
StringBuilder sb = new StringBuilder();
for(int i=0; i <= A.length; i++){
int k = A[i];
while(k > 0){
sb.append("#");
k--;
}
System.out.println(sb.toString());
sb.delete(0, sb.length());

}
}

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

public static void print(int[] A){
if(A.length < 1)
return;
Arrays.sort(A);
StringBuilder sb = new StringBuilder();
for(int i=0; i <= A.length; i++){
int k = A[i];
while(k > 0){
sb.append("#");
k--;
}
System.out.println(sb.toString());
sb.delete(0, sb.length());

}
}

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

``````{ public static void print(int[] A){
if(A.length < 1)
return;
Arrays.sort(A);
StringBuilder sb = new StringBuilder();
for(int i=0; i <= A.length; i++){
int k = A[i];
while(k > 0){
sb.append("#");
k--;
}
System.out.println(sb.toString());
sb.delete(0, sb.length());

}
}``````

}

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

``````public static void print(int[] A){
if(A.length < 1)
return;
Arrays.sort(A);
StringBuilder sb = new StringBuilder();
for(int i=0; i <= A.length; i++){
int k = A[i];
while(k > 0){
sb.append("#");
k--;
}
System.out.println(sb.toString());
sb.delete(0, sb.length());

}
}``````

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

#include <iostream.h>
#include <conio.h>
void main()
{
int i=1;j=1;
clrscr();
do
{
while(j<=i)
{
cout<<'#';
j++;
}
cout<<'\n';
i++;
j=1;
}while(i<=5);
getch();
}

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

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class test
{ public static void main(String[] args)
{
Integer[] a = new Integer[]
{
1, 4, 3, 2,3,5,6,23
};
List<Integer> l = Arrays.asList(a);
Collections.sort(l);
for (int j : l)
{
for (int num = 1; num <= j; num++)
{
System.out.print("#");

}
System.out.println("");
}}}

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

Can someone explain the question and related logic?
I understand that it'll print the following
_#_ _
_##_
_###
####

But how exactly do we get this logic written in answer from given a[i] = n?

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.

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.