Amazon Interview Question for Software Engineer Interns


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");
}

- Anonymous March 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Anonymous March 10, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

#
 ##
 ###
####

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

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;
}

- babesh March 11, 2015 | Flag
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();
	    }
	}

- niraj.nijju March 11, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

you code fails bro...

- mudit March 11, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

your code fails

- mudit March 11, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

hi Mudit,
Where does it fails.

- niraj.nijju March 12, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

your code fails

- jabaraku March 11, 2015 | Flag Reply
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();
        }
        
    }

- jabaraku March 11, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

// 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();
            }

- Swapnil S. March 11, 2015 | Flag
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();
            }

- Swapnil Sankla March 11, 2015 | Flag Reply
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();
            }

- Swapnil S March 11, 2015 | Flag Reply
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();
            }

- swapnil.sankla March 11, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- pbmeldas March 12, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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();
}

- swapnil.sankla March 16, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

this is a question asking do you know insertion sort.

- crowdy March 16, 2015 | Flag Reply
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
        }
        list.add(s);
    }

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

- JW March 21, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can't you use treeset?

- Anon March 22, 2015 | Flag Reply
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();
}

- Jeet March 23, 2015 | Flag Reply
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();
}
}

- Jeet March 23, 2015 | Flag Reply
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();
}

- Jeet March 23, 2015 | Flag Reply
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();

}

- jeet March 23, 2015 | Flag Reply
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();

}

- jeet March 23, 2015 | Flag Reply
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());

}
}

- Ali March 24, 2015 | Flag Reply
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());

}
}

- Ali March 24, 2015 | Flag Reply
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());

        }
    }

}

- Ali March 24, 2015 | Flag Reply
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());

        }
    }

- AK March 24, 2015 | Flag Reply
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();
}

- Dhivakaran April 18, 2015 | Flag Reply
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("");
}}}

- Arunadevi raju May 11, 2015 | Flag Reply
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?

- Ks January 30, 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